#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
#define isinf asdf
char sand[410][410];
int n, m;
bool onstk[410][410];
bool vis[410][410];
int dpU[410][410];
int dpD[410][410];
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
/*
1
2 x 0
3
*/
bool isinf;
int dfs(int x, int y,int last_dir){
onstk[x][y] = 1;
vis[x][y] = 1;
int ret = 1;
int nxt_dir = 0;
if(sand[x][y] == 'Z'){
nxt_dir = 3-last_dir;
} else { // N
nxt_dir = last_dir^1;
}
int dirs[2] = {last_dir, nxt_dir};
for(int d=0; d<2; ++d){
int nx=x+dx[dirs[d]], ny=y+dy[dirs[d]];
if(nx<1 || nx>n || ny<1 || ny>m) continue;
if(!vis[nx][ny]) ret += dfs(nx, ny, dirs[d]);
else if(onstk[nx][ny]) isinf=true;
}
onstk[x][y] = 0;
return ret;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1; i<=n; ++i) scanf("%s", sand[i]+1);
for(int j=1; j<=m; ++j){
memset(vis, 0, sizeof(vis));
isinf = 0;
for(int i=1; i<=n; ++i){
int val = dfs(i, j, 1);
if(isinf) dpU[i][j] = inf;
else dpU[i][j] = dpU[i-1][j] + val;
}
}
for(int j=1; j<=m; ++j){
memset(vis, 0, sizeof(vis));
isinf = 0;
for(int i=n; i>=1; --i){
int val = dfs(i, j, 3);
if(isinf) dpD[i][j] = inf;
else dpD[i][j] = dpD[i+1][j] + val;
}
}
for(int i=1; i<=n; ++i){
for(int j=1; j<=m; ++j){
int v=min(dpU[i][j], dpD[i][j]);
if(v == inf) v=-1;
else v<<=1;
printf("%d", v);
if(j<m) putchar(' ');
}
putchar(10);
}
return 0;
}
Compilation message
sandwich.cpp: In function 'int main()':
sandwich.cpp:48:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
sandwich.cpp:49:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | for(int i=1; i<=n; ++i) scanf("%s", sand[i]+1);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
10 ms |
820 KB |
Output is correct |
7 |
Correct |
5 ms |
588 KB |
Output is correct |
8 |
Correct |
6 ms |
680 KB |
Output is correct |
9 |
Correct |
3 ms |
588 KB |
Output is correct |
10 |
Correct |
6 ms |
588 KB |
Output is correct |
11 |
Correct |
7 ms |
844 KB |
Output is correct |
12 |
Correct |
3 ms |
716 KB |
Output is correct |
13 |
Correct |
5 ms |
716 KB |
Output is correct |
14 |
Correct |
5 ms |
716 KB |
Output is correct |
15 |
Correct |
4 ms |
588 KB |
Output is correct |
16 |
Correct |
3 ms |
588 KB |
Output is correct |
17 |
Correct |
3 ms |
588 KB |
Output is correct |
18 |
Correct |
4 ms |
588 KB |
Output is correct |
19 |
Correct |
4 ms |
588 KB |
Output is correct |
20 |
Correct |
4 ms |
588 KB |
Output is correct |
21 |
Correct |
4 ms |
692 KB |
Output is correct |
22 |
Correct |
4 ms |
588 KB |
Output is correct |
23 |
Correct |
4 ms |
588 KB |
Output is correct |
24 |
Correct |
6 ms |
704 KB |
Output is correct |
25 |
Correct |
6 ms |
588 KB |
Output is correct |
26 |
Correct |
6 ms |
588 KB |
Output is correct |
27 |
Correct |
7 ms |
684 KB |
Output is correct |
28 |
Correct |
8 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
10 ms |
820 KB |
Output is correct |
7 |
Correct |
5 ms |
588 KB |
Output is correct |
8 |
Correct |
6 ms |
680 KB |
Output is correct |
9 |
Correct |
3 ms |
588 KB |
Output is correct |
10 |
Correct |
6 ms |
588 KB |
Output is correct |
11 |
Correct |
7 ms |
844 KB |
Output is correct |
12 |
Correct |
3 ms |
716 KB |
Output is correct |
13 |
Correct |
5 ms |
716 KB |
Output is correct |
14 |
Correct |
5 ms |
716 KB |
Output is correct |
15 |
Correct |
4 ms |
588 KB |
Output is correct |
16 |
Correct |
3 ms |
588 KB |
Output is correct |
17 |
Correct |
3 ms |
588 KB |
Output is correct |
18 |
Correct |
4 ms |
588 KB |
Output is correct |
19 |
Correct |
4 ms |
588 KB |
Output is correct |
20 |
Correct |
4 ms |
588 KB |
Output is correct |
21 |
Correct |
4 ms |
692 KB |
Output is correct |
22 |
Correct |
4 ms |
588 KB |
Output is correct |
23 |
Correct |
4 ms |
588 KB |
Output is correct |
24 |
Correct |
6 ms |
704 KB |
Output is correct |
25 |
Correct |
6 ms |
588 KB |
Output is correct |
26 |
Correct |
6 ms |
588 KB |
Output is correct |
27 |
Correct |
7 ms |
684 KB |
Output is correct |
28 |
Correct |
8 ms |
588 KB |
Output is correct |
29 |
Correct |
10 ms |
516 KB |
Output is correct |
30 |
Correct |
2 ms |
1996 KB |
Output is correct |
31 |
Correct |
4289 ms |
10492 KB |
Output is correct |
32 |
Correct |
4278 ms |
10784 KB |
Output is correct |
33 |
Correct |
309 ms |
4284 KB |
Output is correct |
34 |
Correct |
1300 ms |
3156 KB |
Output is correct |
35 |
Correct |
1099 ms |
2648 KB |
Output is correct |
36 |
Correct |
3 ms |
1996 KB |
Output is correct |
37 |
Correct |
1641 ms |
3184 KB |
Output is correct |
38 |
Correct |
1549 ms |
3268 KB |
Output is correct |
39 |
Correct |
1551 ms |
3048 KB |
Output is correct |
40 |
Correct |
2434 ms |
18196 KB |
Output is correct |
41 |
Correct |
1454 ms |
11608 KB |
Output is correct |
42 |
Correct |
1897 ms |
4200 KB |
Output is correct |
43 |
Correct |
1841 ms |
3924 KB |
Output is correct |
44 |
Correct |
1787 ms |
3888 KB |
Output is correct |
45 |
Correct |
1207 ms |
3224 KB |
Output is correct |
46 |
Correct |
846 ms |
3160 KB |
Output is correct |
47 |
Correct |
962 ms |
3180 KB |
Output is correct |
48 |
Correct |
535 ms |
2800 KB |
Output is correct |
49 |
Correct |
826 ms |
3148 KB |
Output is correct |
50 |
Correct |
987 ms |
3176 KB |
Output is correct |
51 |
Correct |
885 ms |
3144 KB |
Output is correct |
52 |
Correct |
905 ms |
3152 KB |
Output is correct |
53 |
Correct |
844 ms |
3152 KB |
Output is correct |
54 |
Correct |
815 ms |
3016 KB |
Output is correct |
55 |
Correct |
902 ms |
3148 KB |
Output is correct |
56 |
Correct |
894 ms |
3108 KB |
Output is correct |
57 |
Correct |
620 ms |
2964 KB |
Output is correct |
58 |
Correct |
2329 ms |
3636 KB |
Output is correct |
59 |
Correct |
2419 ms |
3568 KB |
Output is correct |
60 |
Correct |
1607 ms |
3056 KB |
Output is correct |
61 |
Correct |
2193 ms |
3460 KB |
Output is correct |
62 |
Correct |
2512 ms |
3180 KB |
Output is correct |
63 |
Correct |
2778 ms |
3308 KB |
Output is correct |