This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N=405;
const int inf=N*N*2;
char mat[N][N];
int mv[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
int was[N][N],sum,ans[N][N];
void DFS(int x, int y, int dir)
{
if(mat[x][y]!='N' && mat[x][y]!='Z') return;
if(was[x][y]==2) return;
if(was[x][y]==1){ sum=inf;return;}
int xr;sum+=2;
if(mat[x][y]=='N') xr=1;
else xr=3;
was[x][y]=1;
DFS(x+mv[dir][0],y+mv[dir][1],dir);
DFS(x+mv[dir^xr][0],y+mv[dir^xr][1],dir^xr);
was[x][y]=2;
}
void cl(){ for(int i=0;i<N;i++) for(int j=0;j<N;j++) was[i][j]=0;sum=0;}
int main()
{
int n,m;
scanf("%i %i",&n,&m);
for(int i=1;i<=n;i++) scanf("%s",mat[i]+1);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
DFS(i,j,2);
ans[i][j]=sum;
}
cl();
for(int j=m;j>=1;j--)
{
DFS(i,j,0);
ans[i][j]=min(ans[i][j],sum);
}
cl();
}
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
{
if(ans[i][j]>=inf) printf("-1 ");
else printf("%i ",ans[i][j]);
if(j==m) printf("\n");
}
return 0;
}
Compilation message (stderr)
sandwich.cpp: In function 'int main()':
sandwich.cpp:25:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&n,&m);
~~~~~^~~~~~~~~~~~~~~
sandwich.cpp:26:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=n;i++) scanf("%s",mat[i]+1);
~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |