Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 4007;
- string s[N];
- int R0[N];
- int R1[N];
- int R2[N];
- int R3[N];
- int n,m;
- queue<pair<pair<int,int>,int> > q;
- int hw=0;
- bool f[N][N];
- bool used[N][N];
- void add_queue(){
- for(int i=0;i<n;++i){
- for(int j=0;j<m;++j){
- if(s[i][j]=='W'){
- f[i][j]=1;
- q.push({{i,j},0});
- hw++;
- break;
- }
- else if(s[i][j]!='.')break;
- }
- }
- for(int i=0;i<n;++i){
- for(int j=m-1;j>=0;j--){
- if(s[i][j]=='E'){
- f[i][j]=1;
- q.push({{i,j},1});
- hw++;
- break;
- }
- else if(s[i][j]!='.')break;
- }
- }
- for(int j=0;j<m;++j){
- for(int i=0;i<n;++i){
- if(s[i][j]=='N'){
- f[i][j]=1;
- q.push({{i,j},2});
- hw++;
- break;
- }
- else if(s[i][j]!='.')break;
- }
- }
- for(int j=0;j<m;++j){
- for(int i=n-1;i>=0;i--){
- if(s[i][j]=='S'){
- f[i][j]=1;
- q.push({{i,j},3});
- hw++;
- break;
- }
- else if(s[i][j]!='.')break;
- }
- }
- }
- int main(){
- ios_base::sync_with_stdio(false);
- // freopen("input.txt","r",stdin);
- cin>>n>>m;
- for(int i=0;i<n;++i)cin>>s[i];
- for(int i=0;i<n;++i)
- for(int j=0;j<m;++j){
- if(s[i][j]=='.')f[i][j]=true;
- }
- add_queue();
- for(int i=0;i<n;++i){
- R0[i]=0;
- R1[i]=m-1;
- }
- for(int i=0;i<m;++i){
- R2[i]=0;
- R3[i]=n-1;
- }
- while(!q.empty()){
- auto cur=q.front();q.pop();
- int x=cur.first.first;
- int y=cur.first.second;
- int tp=cur.second;
- if(used[x][y])continue;
- used[x][y]=true;
- if(tp==0){
- while(R0[x]<m&&f[x][R0[x]]==1)++R0[x];
- if(R0[x]<m){
- int t=R0[x];
- if(s[x][t]=='W'&&!f[x][t]){
- f[x][t]=1;
- hw++;
- q.push({{x,t},0});
- }
- }
- int t=y;
- while(R2[t]<n&&f[R2[t]][t]==1)++R2[t];
- if(R2[t]<n&&s[R2[t]][t]=='N'&&!f[R2[t]][t]){
- f[R2[t]][t]=1;
- hw++;
- q.push({{R2[t],t},2});
- }
- while(R3[t]>=0&&f[R3[t]][t]==1)--R3[t];
- if(R3[t]>=0&&s[R3[t]][t]=='S'&&!f[R3[t]][t]){
- f[R3[t]][t]=1;
- hw++;
- q.push({{R3[t],t},3});
- }
- }
- if(tp==1){
- while(R1[x]>=0&&f[x][R1[x]]==1)--R1[x];
- if(R1[x]>=0){
- int t=R1[x];
- if(s[x][t]=='E'&&!f[x][t]){
- f[x][t]=1;
- hw++;
- q.push({{x,t},1});
- }
- }
- int t=y;
- while(R2[t]<n&&f[R2[t]][t]==1)++R2[t];
- if(R2[t]<n&&s[R2[t]][t]=='N'&&!f[R2[t]][t]){
- f[R2[t]][t]=1;
- hw++;
- q.push({{R2[t],t},2});
- }
- while(R3[t]>=0&&f[R3[t]][t]==1)--R3[t];
- if(R3[t]>=0&&s[R3[t]][t]=='S'&&!f[R3[t]][t]){
- f[R3[t]][t]=1;
- hw++;
- q.push({{R3[t],t},3});
- }
- }
- if(tp==2){
- while(R2[y]<n&&f[R2[y]][y]==1)++R2[y];
- if(R2[y]<n){
- int t=R2[y];
- if(s[t][y]=='N'&&!f[t][y]){
- f[t][y]=1;
- hw++;
- q.push({{t,y},2});
- }}
- int t=x;
- while(R0[t]<m&&f[t][R0[t]]==1)++R0[t];
- if(R0[t]<m&&s[t][R0[t]]=='W'&&!f[t][R0[t]]){
- f[t][R0[t]]=1;
- hw++;
- q.push({{t,R0[t]},0});
- }
- while(R1[t]>=0&&f[t][R1[t]]==1)--R1[t];
- if(R1[t]>=0&&s[t][R1[t]]=='E'&&!f[t][R1[t]]){
- f[t][R1[t]]=1;
- hw++;
- q.push({{t,R1[t]},1});
- }
- }
- if(tp==3){
- while(R3[y]>=0&&f[R3[y]][y]==1)--R3[y];
- if(R3[y]>=0){
- int t=R3[y];
- if(s[t][y]=='S'&&!f[t][y]){
- f[t][y]=1;
- hw++;
- q.push({{t,y},3});
- }}
- int t=x;
- while(R0[t]<m&&f[t][R0[t]]==1)++R0[t];
- if(R0[t]<m&&s[t][R0[t]]=='W'&&!f[t][R0[t]]){
- f[t][R0[t]]=1;
- hw++;
- q.push({{t,R0[t]},0});
- }
- while(R1[t]>=0&&f[t][R1[t]]==1)--R1[t];
- if(R1[t]>=0&&s[t][R1[t]]=='E'&&!f[t][R1[t]]){
- f[t][R1[t]]=1;
- hw++;
- q.push({{t,R1[t]},1});
- }
- }
- }
- cout<<hw<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement