#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[410][410];
int vis[410][410];
int chk[410][410];
int num = 0;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int dfs(int x, int y, int d) {
vis[x][y] = num << 1 | d;
for (int i=0; i<2; i++) {
int t = (d<<1) + a[x][y] + i; if (t >= 4) t -= 4;
int nx = x + dx[t];
int ny = y + dy[t];
if (nx <= 0 || nx > n || ny <= 0 || ny > m) continue;
int nd = (t >= a[nx][ny] && t <= 1+a[nx][ny]) ^ 1;
if (vis[nx][ny] == (num << 1 | nd)) {
if (chk[nx][ny] == num) continue;
return 0;
}
if (vis[nx][ny] == (num << 1 | nd ^ 1)) return 0;
if (!dfs(nx, ny, nd)) return 0;
}
chk[x][y] = num;
return 1;
}
int solve(int sx, int sy, int sd) {
num ++;
if (!dfs(sx, sy, sd)) return -1;
int ans = 0;
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
ans += (vis[i][j] >= (num << 1));
}
}
return ans << 1;
}
int main() {
scanf("%d %d", &n, &m);
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
char c;
scanf(" %c", &c);
if (c == 'N') a[i][j] = 0;
else a[i][j] = 1;
}
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
int t1 = solve(i, j, 0), t2 = solve(i, j, 1);
if (t1 == -1) printf("%d ", t2);
else if (t2 == -1) printf("%d ", t1);
else printf("%d ", min(t1, t2));
}
printf("\n");
}
}
Compilation message
sandwich.cpp: In function 'int dfs(int, int, int)':
sandwich.cpp:26:43: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
26 | if (vis[nx][ny] == (num << 1 | nd ^ 1)) return 0;
| ~~~^~~
sandwich.cpp: In function 'int main()':
sandwich.cpp:46:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
sandwich.cpp:50:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf(" %c", &c);
| ~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
70 ms |
696 KB |
Output is correct |
8 |
Correct |
82 ms |
676 KB |
Output is correct |
9 |
Correct |
42 ms |
648 KB |
Output is correct |
10 |
Correct |
83 ms |
604 KB |
Output is correct |
11 |
Correct |
101 ms |
848 KB |
Output is correct |
12 |
Correct |
34 ms |
604 KB |
Output is correct |
13 |
Correct |
109 ms |
712 KB |
Output is correct |
14 |
Correct |
107 ms |
604 KB |
Output is correct |
15 |
Correct |
55 ms |
676 KB |
Output is correct |
16 |
Correct |
46 ms |
604 KB |
Output is correct |
17 |
Correct |
51 ms |
848 KB |
Output is correct |
18 |
Correct |
46 ms |
676 KB |
Output is correct |
19 |
Correct |
47 ms |
600 KB |
Output is correct |
20 |
Correct |
45 ms |
676 KB |
Output is correct |
21 |
Correct |
42 ms |
600 KB |
Output is correct |
22 |
Correct |
43 ms |
600 KB |
Output is correct |
23 |
Correct |
47 ms |
600 KB |
Output is correct |
24 |
Correct |
103 ms |
724 KB |
Output is correct |
25 |
Correct |
104 ms |
604 KB |
Output is correct |
26 |
Correct |
108 ms |
600 KB |
Output is correct |
27 |
Correct |
107 ms |
684 KB |
Output is correct |
28 |
Correct |
103 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
70 ms |
696 KB |
Output is correct |
8 |
Correct |
82 ms |
676 KB |
Output is correct |
9 |
Correct |
42 ms |
648 KB |
Output is correct |
10 |
Correct |
83 ms |
604 KB |
Output is correct |
11 |
Correct |
101 ms |
848 KB |
Output is correct |
12 |
Correct |
34 ms |
604 KB |
Output is correct |
13 |
Correct |
109 ms |
712 KB |
Output is correct |
14 |
Correct |
107 ms |
604 KB |
Output is correct |
15 |
Correct |
55 ms |
676 KB |
Output is correct |
16 |
Correct |
46 ms |
604 KB |
Output is correct |
17 |
Correct |
51 ms |
848 KB |
Output is correct |
18 |
Correct |
46 ms |
676 KB |
Output is correct |
19 |
Correct |
47 ms |
600 KB |
Output is correct |
20 |
Correct |
45 ms |
676 KB |
Output is correct |
21 |
Correct |
42 ms |
600 KB |
Output is correct |
22 |
Correct |
43 ms |
600 KB |
Output is correct |
23 |
Correct |
47 ms |
600 KB |
Output is correct |
24 |
Correct |
103 ms |
724 KB |
Output is correct |
25 |
Correct |
104 ms |
604 KB |
Output is correct |
26 |
Correct |
108 ms |
600 KB |
Output is correct |
27 |
Correct |
107 ms |
684 KB |
Output is correct |
28 |
Correct |
103 ms |
600 KB |
Output is correct |
29 |
Correct |
3 ms |
348 KB |
Output is correct |
30 |
Correct |
4 ms |
2396 KB |
Output is correct |
31 |
Correct |
129 ms |
2244 KB |
Output is correct |
32 |
Correct |
80 ms |
2128 KB |
Output is correct |
33 |
Correct |
28 ms |
1628 KB |
Output is correct |
34 |
Execution timed out |
8019 ms |
2952 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |