B : 三明治 2021-01-17 14:25:53
#include<stdio.h> #include<string.h>//memset(void *s, int v, size_t n)函数的头文件 #include<limits.h>//INT_MAX的头文件 INT_MAX=2147483647 INT_MIN=-2147483648 #define N 405//宏定义 char mp[N][N];//三明治
//n行,m列, int n, m, res, flag, ans[N][N], vis[N][N]; int fx[]={0,-1,0,1}; int fy[]={-1,0,1,0}; int min(int a, int b){ if (a<=b) return a; else return b; } int check(int x, int y){ return 0<=x && x<n && 0<=y && y<m; } void dfs(int x, int y, int d){ if(vis[x][y]==-1){ flag=1; return; } if(vis[x][y]==1) return; res+=2; vis[x][y]=-1; int p = ((mp[x][y]=='N') ? 3:1); if(check( x-fx[d], y-fy[d] )) dfs(x-fx[d], y-fy[d],d); if(check( x-fx[d^p], y-fy[d^p] )) dfs(x-fx[d^p], y-fy[d^p], d^p); vis[x][y]=1; } int main(){ scanf("%d %d", &n, &m); for(int i=0; i<n; ++i) scanf("%s", &mp[i]); for(int i=0; i<n; ++i){ flag = res = 0; memset(vis, 0, sizeof(vis)); for(int j=0; j<m; ++j){ if(!flag) dfs(i,j,2); ans[i][j]=flag ? INT_MAX:res; } flag = res = 0; memset(vis, 0, sizeof(vis)); for(int j=m-1; j>=0; --j){ if(!flag) dfs(i,j,0); ans[i][j]=min(ans[i][j], flag ? INT_MAX:res); } } for(int i=0; i<n; ++i){ for(int j=0; j<m; ++j) printf("%d ", ans[i][j]==INT_MAX ? -1 : ans[i][j]); printf(" "); } return 0; }