fork download
  1. #include<bits/stdc++.h>
  2. #define lli long long int
  3. using namespace std;
  4. char str[12][12];
  5. int vis[12][12];
  6. int main()
  7. {
  8. ios::sync_with_stdio(false);
  9. int w,h,p1,p2;
  10. cin>>w>>h;
  11. for(int i=1; i<=w; i++)
  12. {
  13. for(int j=1; j<=h; j++)
  14. {
  15. cin>>str[i][j];
  16. if(str[i][j]=='U' ||str[i][j]=='L' || str[i][j]=='R' || str[i][j]=='D')
  17. {
  18. p1=i;
  19. p2=j;
  20. }
  21. }
  22. }
  23. int ans=1;
  24. vis[p1][p2]=1;
  25. while(1)
  26. {
  27. bool flag=0;
  28. if(str[p1][p2]=='U')
  29. {
  30. if(p1>1 &&(str[p1-1][p2]!='*' ))
  31. {
  32. p1=p1-1;
  33. str[p1][p2]='U';
  34. if(!vis[p1][p2])
  35. ans++;
  36. vis[p1][p2]++;
  37. flag=1;
  38. }
  39. else if(p2<h &&(str[p1][p2+1]!='*' ))
  40. {
  41. p2=p2+1;
  42. str[p1][p2]='R';
  43. if(!vis[p1][p2])
  44. ans++;
  45. vis[p1][p2]++;
  46. flag=1;
  47. }
  48. else if(p1<w && (str[p1+1][p2]!='*' ))
  49. {
  50. p1=p1+1;
  51. str[p1][p2]='D';
  52. if(!vis[p1][p2])
  53. ans++;
  54. vis[p1][p2]++;
  55. flag=1;
  56. }
  57. else if(p2>1 && (str[p1][p2-1]!='*' ))
  58. {
  59. p2=p2-1;
  60. str[p1][p2]='L';
  61. if(!vis[p1][p2])
  62. ans++;
  63. vis[p1][p2]++;
  64. flag=1;
  65. }
  66. }
  67. else if(str[p1][p2]=='R')
  68. {
  69. if(p2<h &&(str[p1][p2+1]!='*' ))
  70. {
  71. p2=p2+1;
  72. str[p1][p2]='R';
  73. if(!vis[p1][p2])
  74. ans++;
  75. vis[p1][p2]++;
  76. flag=1;
  77. }
  78. else if(p1<w && (str[p1+1][p2]!='*'))
  79. {
  80. p1=p1+1;
  81. str[p1][p2]='D';
  82. if(!vis[p1][p2])
  83. ans++;
  84. vis[p1][p2]++;
  85. flag=1;
  86. }
  87. else if(p2>1 && (str[p1][p2-1]!='*' ))
  88. {
  89. p2=p2-1;
  90. str[p1][p2]='L';
  91. if(!vis[p1][p2])
  92. ans++;
  93. vis[p1][p2]++;
  94. flag=1;
  95. }
  96. else if(p1>1 &&(str[p1-1][p2]!='*'))
  97. {
  98. p1=p1-1;
  99. str[p1][p2]='U';
  100. if(!vis[p1][p2])
  101. ans++;
  102. vis[p1][p2]++;
  103. flag=1;
  104. }
  105.  
  106. }
  107. else if(str[p1][p2]=='D')
  108. {
  109. if(p1<w && (str[p1+1][p2]!='*' ))
  110. {
  111. p1=p1+1;
  112. str[p1][p2]='D';
  113. if(!vis[p1][p2])
  114. ans++;
  115. vis[p1][p2]++;
  116. flag=1;
  117. }
  118. else if(p2>1 && (str[p1][p2-1]!='*'))
  119. {
  120. p2=p2-1;
  121. str[p1][p2]='L';
  122. if(!vis[p1][p2])
  123. ans++;
  124. vis[p1][p2]++;
  125. flag=1;
  126. }
  127. else if(p1>1 &&(str[p1-1][p2]!='*' ))
  128. {
  129. p1=p1-1;
  130. str[p1][p2]='U';
  131. if(!vis[p1][p2])
  132. ans++;
  133. vis[p1][p2]++;
  134. flag=1;
  135. }
  136. else if(p2<h &&(str[p1][p2+1]!='*' ))
  137. {
  138. p2=p2+1;
  139. str[p1][p2]='R';
  140. if(!vis[p1][p2])
  141. ans++;
  142. vis[p1][p2]++;
  143. flag=1;
  144. }
  145. }
  146. else if(str[p1][p2]=='L')
  147. {
  148. if(p2>1 && (str[p1][p2-1]!='*' ))
  149. {
  150. p2=p2-1;
  151. str[p1][p2]='L';
  152. if(!vis[p1][p2])
  153. ans++;
  154. vis[p1][p2]++;
  155. flag=1;
  156. }
  157. else if(p1>1 &&(str[p1-1][p2]!='*' ))
  158. {
  159. p1=p1-1;
  160. str[p1][p2]='U';
  161. if(!vis[p1][p2])
  162. ans++;
  163. vis[p1][p2]++;
  164. flag=1;
  165. }
  166. else if(p2<h &&(str[p1][p2+1]!='*'))
  167. {
  168. p2=p2+1;
  169. str[p1][p2]='R';
  170. if(!vis[p1][p2])
  171. ans++;
  172. vis[p1][p2]++;
  173. flag=1;
  174. }
  175. else if(p1<w && (str[p1+1][p2]!='*'))
  176. {
  177. p1=p1+1;
  178. str[p1][p2]='D';
  179. if(!vis[p1][p2])
  180. ans++;
  181. vis[p1][p2]++;
  182. flag=1;
  183. }
  184. }
  185. //cout<<str[p1][p2]<<endl;
  186. if(vis[p1][p2]>10)
  187. break;
  188. if(!flag)
  189. {
  190. break;
  191. }
  192. }
  193. cout<<ans<<endl;
  194. return 0;
  195. }
Runtime error #stdin #stdout 0s 3452KB
stdin
Standard input is empty
stdout
Standard output is empty