Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 4007;
  4. string s[N];
  5. int R0[N];
  6. int R1[N];
  7. int R2[N];
  8. int R3[N];
  9. int n,m;
  10. queue<pair<pair<int,int>,int> > q;
  11. int hw=0;
  12. bool f[N][N];
  13. bool used[N][N];
  14. void add_queue(){
  15. for(int i=0;i<n;++i){
  16. for(int j=0;j<m;++j){
  17. if(s[i][j]=='W'){
  18. f[i][j]=1;
  19. q.push({{i,j},0});
  20. hw++;
  21. break;
  22. }
  23. else if(s[i][j]!='.')break;
  24. }
  25. }
  26. for(int i=0;i<n;++i){
  27. for(int j=m-1;j>=0;j--){
  28. if(s[i][j]=='E'){
  29. f[i][j]=1;
  30. q.push({{i,j},1});
  31. hw++;
  32. break;
  33. }
  34. else if(s[i][j]!='.')break;
  35. }
  36. }
  37. for(int j=0;j<m;++j){
  38. for(int i=0;i<n;++i){
  39. if(s[i][j]=='N'){
  40. f[i][j]=1;
  41. q.push({{i,j},2});
  42. hw++;
  43. break;
  44. }
  45. else if(s[i][j]!='.')break;
  46. }
  47. }
  48. for(int j=0;j<m;++j){
  49. for(int i=n-1;i>=0;i--){
  50. if(s[i][j]=='S'){
  51. f[i][j]=1;
  52. q.push({{i,j},3});
  53. hw++;
  54. break;
  55. }
  56. else if(s[i][j]!='.')break;
  57. }
  58. }
  59. }
  60. int main(){
  61. ios_base::sync_with_stdio(false);
  62. // freopen("input.txt","r",stdin);
  63. cin>>n>>m;
  64. for(int i=0;i<n;++i)cin>>s[i];
  65. for(int i=0;i<n;++i)
  66. for(int j=0;j<m;++j){
  67. if(s[i][j]=='.')f[i][j]=true;
  68. }
  69. add_queue();
  70. for(int i=0;i<n;++i){
  71. R0[i]=0;
  72. R1[i]=m-1;
  73. }
  74. for(int i=0;i<m;++i){
  75. R2[i]=0;
  76. R3[i]=n-1;
  77. }
  78. while(!q.empty()){
  79. auto cur=q.front();q.pop();
  80. int x=cur.first.first;
  81. int y=cur.first.second;
  82. int tp=cur.second;
  83. if(used[x][y])continue;
  84. used[x][y]=true;
  85. if(tp==0){
  86. while(R0[x]<m&&f[x][R0[x]]==1)++R0[x];
  87. if(R0[x]<m){
  88. int t=R0[x];
  89. if(s[x][t]=='W'&&!f[x][t]){
  90. f[x][t]=1;
  91. hw++;
  92. q.push({{x,t},0});
  93. }
  94. }
  95. int t=y;
  96. while(R2[t]<n&&f[R2[t]][t]==1)++R2[t];
  97. if(R2[t]<n&&s[R2[t]][t]=='N'&&!f[R2[t]][t]){
  98. f[R2[t]][t]=1;
  99. hw++;
  100. q.push({{R2[t],t},2});
  101. }
  102.  
  103. while(R3[t]>=0&&f[R3[t]][t]==1)--R3[t];
  104. if(R3[t]>=0&&s[R3[t]][t]=='S'&&!f[R3[t]][t]){
  105. f[R3[t]][t]=1;
  106. hw++;
  107. q.push({{R3[t],t},3});
  108. }
  109.  
  110. }
  111. if(tp==1){
  112. while(R1[x]>=0&&f[x][R1[x]]==1)--R1[x];
  113. if(R1[x]>=0){
  114. int t=R1[x];
  115. if(s[x][t]=='E'&&!f[x][t]){
  116. f[x][t]=1;
  117. hw++;
  118. q.push({{x,t},1});
  119. }
  120. }
  121. int t=y;
  122. while(R2[t]<n&&f[R2[t]][t]==1)++R2[t];
  123. if(R2[t]<n&&s[R2[t]][t]=='N'&&!f[R2[t]][t]){
  124. f[R2[t]][t]=1;
  125. hw++;
  126. q.push({{R2[t],t},2});
  127. }
  128.  
  129. while(R3[t]>=0&&f[R3[t]][t]==1)--R3[t];
  130. if(R3[t]>=0&&s[R3[t]][t]=='S'&&!f[R3[t]][t]){
  131. f[R3[t]][t]=1;
  132. hw++;
  133. q.push({{R3[t],t},3});
  134. }
  135. }
  136. if(tp==2){
  137. while(R2[y]<n&&f[R2[y]][y]==1)++R2[y];
  138. if(R2[y]<n){
  139. int t=R2[y];
  140. if(s[t][y]=='N'&&!f[t][y]){
  141. f[t][y]=1;
  142. hw++;
  143. q.push({{t,y},2});
  144. }}
  145. int t=x;
  146. while(R0[t]<m&&f[t][R0[t]]==1)++R0[t];
  147. if(R0[t]<m&&s[t][R0[t]]=='W'&&!f[t][R0[t]]){
  148. f[t][R0[t]]=1;
  149. hw++;
  150. q.push({{t,R0[t]},0});
  151. }
  152.  
  153. while(R1[t]>=0&&f[t][R1[t]]==1)--R1[t];
  154. if(R1[t]>=0&&s[t][R1[t]]=='E'&&!f[t][R1[t]]){
  155. f[t][R1[t]]=1;
  156. hw++;
  157. q.push({{t,R1[t]},1});
  158. }
  159.  
  160. }
  161. if(tp==3){
  162. while(R3[y]>=0&&f[R3[y]][y]==1)--R3[y];
  163. if(R3[y]>=0){
  164. int t=R3[y];
  165. if(s[t][y]=='S'&&!f[t][y]){
  166. f[t][y]=1;
  167. hw++;
  168. q.push({{t,y},3});
  169. }}
  170. int t=x;
  171. while(R0[t]<m&&f[t][R0[t]]==1)++R0[t];
  172. if(R0[t]<m&&s[t][R0[t]]=='W'&&!f[t][R0[t]]){
  173. f[t][R0[t]]=1;
  174. hw++;
  175. q.push({{t,R0[t]},0});
  176. }
  177.  
  178. while(R1[t]>=0&&f[t][R1[t]]==1)--R1[t];
  179. if(R1[t]>=0&&s[t][R1[t]]=='E'&&!f[t][R1[t]]){
  180. f[t][R1[t]]=1;
  181. hw++;
  182. q.push({{t,R1[t]},1});
  183. }
  184. }
  185. }
  186. cout<<hw<<endl;
  187. return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement