#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 400;
const int INF = 1e7;
int N, M;
char A[MAXN+10][MAXN+10];
int ans[MAXN+10][MAXN+10];
int vis[MAXN+10][MAXN+10];
int cnt;
void dfs(int y, int x)
{
cnt++;
if(A[y][x]=='N')
{
if(vis[y][x]==1)
{
if(vis[y-1][x]>0) cnt=-INF;
if(y!=1 && !vis[y-1][x])
{
vis[y-1][x]=1;
dfs(y-1, x);
}
if(vis[y][x+1]>0) cnt=-INF;
if(x!=M && !vis[y][x+1])
{
if(A[y][x+1]=='N') vis[y][x+1]=1;
else vis[y][x+1]=2;
dfs(y, x+1);
}
}
if(vis[y][x]==2)
{
if(vis[y+1][x]>0) cnt=-INF;
if(y!=N && !vis[y+1][x])
{
vis[y+1][x]=2;
dfs(y+1, x);
}
if(vis[y][x-1]>0) cnt=-INF;
if(x!=1 && !vis[y][x-1])
{
if(A[y][x-1]=='N') vis[y][x-1]=2;
else vis[y][x-1]=1;
dfs(y, x-1);
}
}
}
else
{
if(vis[y][x]==1)
{
if(vis[y-1][x]>0) cnt=-INF;
if(y!=1 && !vis[y-1][x])
{
vis[y-1][x]=1;
dfs(y-1, x);
}
if(vis[y][x-1]>0) cnt=-INF;
if(x!=1 && !vis[y][x-1])
{
if(A[y][x-1]=='N') vis[y][x-1]=2;
else vis[y][x-1]=1;
dfs(y, x-1);
}
}
if(vis[y][x]==2)
{
if(vis[y+1][x]>0) cnt=-INF;
if(y!=N && !vis[y+1][x])
{
vis[y+1][x]=2;
dfs(y+1, x);
}
if(vis[y][x+1]>0) cnt=-INF;
if(x!=M && !vis[y][x+1])
{
if(A[y][x+1]=='N') vis[y][x+1]=1;
else vis[y][x+1]=2;
dfs(y, x+1);
}
}
}
vis[y][x]=-1;
}
int main()
{
int i, j, p, q;
scanf("%d%d", &N, &M);
for(i=1; i<=N; i++) scanf(" %s", A[i]+1);
for(i=1; i<=N; i++) for(j=1; j<=M; j++) ans[i][j]=INF;
for(i=1; i<=N; i++)
{
for(j=1; j<=M; j++)
{
for(p=1; p<=N; p++) for(q=1; q<=M; q++) vis[p][q]=0;
cnt=0; vis[i][j]=1;
dfs(i, j);
if(cnt>=0) ans[i][j]=min(ans[i][j], cnt);
for(p=1; p<=N; p++) for(q=1; q<=M; q++) vis[p][q]=0;
cnt=0; vis[i][j]=2;
dfs(i, j);
if(cnt>=0) ans[i][j]=min(ans[i][j], cnt);
if(ans[i][j]==INF) printf("-1 ");
else printf("%d ", ans[i][j]*2);
}
printf("\n");
}
}
Compilation message
sandwich.cpp: In function 'int main()':
sandwich.cpp:98:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~
sandwich.cpp:99:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=N; i++) scanf(" %s", A[i]+1);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
245 ms |
640 KB |
Output is correct |
7 |
Correct |
70 ms |
512 KB |
Output is correct |
8 |
Correct |
74 ms |
512 KB |
Output is correct |
9 |
Correct |
58 ms |
512 KB |
Output is correct |
10 |
Correct |
83 ms |
632 KB |
Output is correct |
11 |
Correct |
149 ms |
512 KB |
Output is correct |
12 |
Correct |
45 ms |
504 KB |
Output is correct |
13 |
Correct |
116 ms |
512 KB |
Output is correct |
14 |
Correct |
118 ms |
512 KB |
Output is correct |
15 |
Correct |
45 ms |
512 KB |
Output is correct |
16 |
Correct |
41 ms |
512 KB |
Output is correct |
17 |
Correct |
55 ms |
512 KB |
Output is correct |
18 |
Correct |
58 ms |
512 KB |
Output is correct |
19 |
Correct |
47 ms |
512 KB |
Output is correct |
20 |
Correct |
52 ms |
512 KB |
Output is correct |
21 |
Correct |
50 ms |
512 KB |
Output is correct |
22 |
Correct |
46 ms |
512 KB |
Output is correct |
23 |
Correct |
51 ms |
512 KB |
Output is correct |
24 |
Correct |
130 ms |
512 KB |
Output is correct |
25 |
Correct |
120 ms |
512 KB |
Output is correct |
26 |
Correct |
129 ms |
632 KB |
Output is correct |
27 |
Correct |
72 ms |
512 KB |
Output is correct |
28 |
Correct |
97 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
245 ms |
640 KB |
Output is correct |
7 |
Correct |
70 ms |
512 KB |
Output is correct |
8 |
Correct |
74 ms |
512 KB |
Output is correct |
9 |
Correct |
58 ms |
512 KB |
Output is correct |
10 |
Correct |
83 ms |
632 KB |
Output is correct |
11 |
Correct |
149 ms |
512 KB |
Output is correct |
12 |
Correct |
45 ms |
504 KB |
Output is correct |
13 |
Correct |
116 ms |
512 KB |
Output is correct |
14 |
Correct |
118 ms |
512 KB |
Output is correct |
15 |
Correct |
45 ms |
512 KB |
Output is correct |
16 |
Correct |
41 ms |
512 KB |
Output is correct |
17 |
Correct |
55 ms |
512 KB |
Output is correct |
18 |
Correct |
58 ms |
512 KB |
Output is correct |
19 |
Correct |
47 ms |
512 KB |
Output is correct |
20 |
Correct |
52 ms |
512 KB |
Output is correct |
21 |
Correct |
50 ms |
512 KB |
Output is correct |
22 |
Correct |
46 ms |
512 KB |
Output is correct |
23 |
Correct |
51 ms |
512 KB |
Output is correct |
24 |
Correct |
130 ms |
512 KB |
Output is correct |
25 |
Correct |
120 ms |
512 KB |
Output is correct |
26 |
Correct |
129 ms |
632 KB |
Output is correct |
27 |
Correct |
72 ms |
512 KB |
Output is correct |
28 |
Correct |
97 ms |
512 KB |
Output is correct |
29 |
Correct |
7 ms |
384 KB |
Output is correct |
30 |
Correct |
8 ms |
1792 KB |
Output is correct |
31 |
Execution timed out |
8067 ms |
5888 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |