#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
char matrix[404][404];
int visited[404][404];
int dp[404][404], dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, 1, -1}, n, m;
bool valid(int x, int y){
return ((1<=x && x<=n) && (1<=y && y<=m));
}
int cnt;
bool dfs(int x, int y, int c){
if (visited[x][y]) return visited[x][y]==1e9;
visited[x][y] = c+1;
cnt++;
bool ret = 1;
int nx1 = x+dx[c], nx2 = y+dy[c];
if (valid(nx1, nx2)){
ret &= dfs(nx1, nx2, c);
}
int d = 2;
if (!c && matrix[x][y]=='Z') d = 3;
if (c && matrix[x][y]=='N') d = 3;
int nx3 = x+dx[d], nx4 = y+dy[d];
if (valid(nx3, nx4)){
if (d==2 && matrix[nx3][nx4]=='N') ret &= dfs(nx3, nx4, 0);
else if (d==3 && matrix[nx3][nx4]=='Z') ret &= dfs(nx3, nx4, 0);
else ret &= dfs(nx3, nx4, 1);
}
visited[x][y] = 1e9;
return ret;
}
int main(){
scanf("%d %d", &n, &m);
for (int i=1;i<=n;i++){
scanf("%s", matrix[i]+1);
for (int j=1;j<=m;j++) dp[i][j] = 1e9;
}
for (int i=1;i<=n;i++){
for (int i=1;i<=n;i++) fill(visited[i]+1, visited[i]+m+1, 0);
cnt = 0;
for (int j=1;j<=m;j++){
cnt++;
visited[i][j] = 1;
if (matrix[i][j]=='N'){
if (i+1<=n && !dfs(i+1, j, 1)) break;
dp[i][j] = min(dp[i][j], cnt);
}
if (matrix[i][j]=='Z'){
if (i-1>0 && !dfs(i-1, j, 0)) break;
dp[i][j] = min(dp[i][j], cnt);
}
visited[i][j] = 1e9;
}
}
for (int i=1;i<=n;i++){
for (int i=1;i<=n;i++) fill(visited[i]+1, visited[i]+m+1, 0);
cnt = 0;
for (int j=m;j;j--){
cnt++;
visited[i][j] = 1;
if (matrix[i][j]=='Z'){
if (i+1<=n && !dfs(i+1, j, 1)) break;
dp[i][j] = min(dp[i][j], cnt);
}
if (matrix[i][j]=='N'){
if (i-1>0 && !dfs(i-1, j, 0)) break;
dp[i][j] = min(dp[i][j], cnt);
}
visited[i][j] = 1e9;
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
int ans = dp[i][j]<<1;
if (ans==2e9) ans = -1;
printf("%d ", ans);
}
printf("\n");
}
return 0;
}
Compilation message
sandwich.cpp: In function 'int main()':
sandwich.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
sandwich.cpp:39:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | scanf("%s", matrix[i]+1);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
8 ms |
584 KB |
Output is correct |
7 |
Correct |
4 ms |
460 KB |
Output is correct |
8 |
Correct |
5 ms |
460 KB |
Output is correct |
9 |
Correct |
3 ms |
460 KB |
Output is correct |
10 |
Correct |
5 ms |
460 KB |
Output is correct |
11 |
Correct |
5 ms |
488 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
5 ms |
460 KB |
Output is correct |
14 |
Correct |
5 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
460 KB |
Output is correct |
16 |
Correct |
3 ms |
460 KB |
Output is correct |
17 |
Correct |
3 ms |
460 KB |
Output is correct |
18 |
Correct |
3 ms |
460 KB |
Output is correct |
19 |
Correct |
3 ms |
460 KB |
Output is correct |
20 |
Correct |
3 ms |
588 KB |
Output is correct |
21 |
Correct |
3 ms |
460 KB |
Output is correct |
22 |
Correct |
3 ms |
460 KB |
Output is correct |
23 |
Correct |
3 ms |
460 KB |
Output is correct |
24 |
Correct |
5 ms |
460 KB |
Output is correct |
25 |
Correct |
5 ms |
460 KB |
Output is correct |
26 |
Correct |
5 ms |
460 KB |
Output is correct |
27 |
Correct |
3 ms |
460 KB |
Output is correct |
28 |
Correct |
5 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
8 ms |
584 KB |
Output is correct |
7 |
Correct |
4 ms |
460 KB |
Output is correct |
8 |
Correct |
5 ms |
460 KB |
Output is correct |
9 |
Correct |
3 ms |
460 KB |
Output is correct |
10 |
Correct |
5 ms |
460 KB |
Output is correct |
11 |
Correct |
5 ms |
488 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
5 ms |
460 KB |
Output is correct |
14 |
Correct |
5 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
460 KB |
Output is correct |
16 |
Correct |
3 ms |
460 KB |
Output is correct |
17 |
Correct |
3 ms |
460 KB |
Output is correct |
18 |
Correct |
3 ms |
460 KB |
Output is correct |
19 |
Correct |
3 ms |
460 KB |
Output is correct |
20 |
Correct |
3 ms |
588 KB |
Output is correct |
21 |
Correct |
3 ms |
460 KB |
Output is correct |
22 |
Correct |
3 ms |
460 KB |
Output is correct |
23 |
Correct |
3 ms |
460 KB |
Output is correct |
24 |
Correct |
5 ms |
460 KB |
Output is correct |
25 |
Correct |
5 ms |
460 KB |
Output is correct |
26 |
Correct |
5 ms |
460 KB |
Output is correct |
27 |
Correct |
3 ms |
460 KB |
Output is correct |
28 |
Correct |
5 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
6 ms |
1740 KB |
Output is correct |
31 |
Correct |
4043 ms |
7436 KB |
Output is correct |
32 |
Correct |
4100 ms |
7248 KB |
Output is correct |
33 |
Correct |
842 ms |
3012 KB |
Output is correct |
34 |
Correct |
1149 ms |
2792 KB |
Output is correct |
35 |
Correct |
1000 ms |
2296 KB |
Output is correct |
36 |
Correct |
33 ms |
1740 KB |
Output is correct |
37 |
Correct |
1407 ms |
2812 KB |
Output is correct |
38 |
Correct |
1353 ms |
2888 KB |
Output is correct |
39 |
Correct |
1403 ms |
2672 KB |
Output is correct |
40 |
Correct |
1935 ms |
3004 KB |
Output is correct |
41 |
Correct |
754 ms |
2004 KB |
Output is correct |
42 |
Correct |
1662 ms |
3404 KB |
Output is correct |
43 |
Correct |
1583 ms |
3236 KB |
Output is correct |
44 |
Correct |
1633 ms |
3432 KB |
Output is correct |
45 |
Correct |
1129 ms |
2780 KB |
Output is correct |
46 |
Correct |
745 ms |
2776 KB |
Output is correct |
47 |
Correct |
1070 ms |
2800 KB |
Output is correct |
48 |
Correct |
587 ms |
2432 KB |
Output is correct |
49 |
Correct |
815 ms |
2788 KB |
Output is correct |
50 |
Correct |
874 ms |
2792 KB |
Output is correct |
51 |
Correct |
764 ms |
2776 KB |
Output is correct |
52 |
Correct |
798 ms |
2788 KB |
Output is correct |
53 |
Correct |
829 ms |
2728 KB |
Output is correct |
54 |
Correct |
739 ms |
2644 KB |
Output is correct |
55 |
Correct |
772 ms |
2776 KB |
Output is correct |
56 |
Correct |
809 ms |
2776 KB |
Output is correct |
57 |
Correct |
614 ms |
2472 KB |
Output is correct |
58 |
Correct |
1953 ms |
3768 KB |
Output is correct |
59 |
Correct |
2019 ms |
3524 KB |
Output is correct |
60 |
Correct |
1435 ms |
3220 KB |
Output is correct |
61 |
Correct |
2092 ms |
7872 KB |
Output is correct |
62 |
Correct |
1037 ms |
2800 KB |
Output is correct |
63 |
Correct |
1564 ms |
2980 KB |
Output is correct |