#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <functional>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
typedef long long llong;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<llong, llong> pll;
int r, c;
char grid[501][501];
struct _edge {
int t, x, y;
};
vector<_edge> edge[2][501][501];
int used[2][500][500];
int fin[2][500][500];
const int inf = 1e6;
int dfs(int t, int x, int y) {
if (fin[t][x][y]) return 0;
if (used[t][x][y]) return inf;
used[t][x][y] = 1;
int ret = 1;
for (_edge i : edge[t][x][y]) {
ret += dfs(i.t, i.x, i.y);
ret = min(ret, inf);
}
fin[t][x][y] = 1;
return ret;
}
int ans[500][500];
int main() {
scanf("%d%d", &r, &c);
for (int i = 0; i < r; ++i) {
scanf("%s", grid[i]);
}
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
if (j) edge[0][i][j].push_back({ 0, i, j - 1 });
if (i + 1 < r) edge[grid[i][j] != 'N'][i][j].push_back({ grid[i + 1][j] != 'N', i + 1, j });
if (j + 1 < c) edge[1][i][j].push_back({ 1, i, j + 1 } );
if (i) edge[grid[i][j] != 'Z'][i][j].push_back({ grid[i - 1][j] != 'Z', i - 1, j });
}
}
for (int i = 0; i < r; ++i) {
for (int j = 0; j < r; ++j) {
for (int k = 0; k < c; ++k) {
used[0][j][k] = 0;
used[1][j][k] = 0;
fin[0][j][k] = 0;
fin[1][j][k] = 0;
}
}
int sum = 0;
for (int j = 0; j < c; ++j) {
sum += dfs(0, i, j);
sum = min(sum, inf);
ans[i][j] = sum;
}
}
for (int i = 0; i < r; ++i) {
for (int j = 0; j < r; ++j) {
for (int k = 0; k < c; ++k) {
used[0][j][k] = 0;
used[1][j][k] = 0;
fin[0][j][k] = 0;
fin[1][j][k] = 0;
}
}
int sum = 0;
for (int j = c; j--; ) {
sum += dfs(1, i, j);
sum = min(sum, inf);
ans[i][j] = min(ans[i][j], sum);
}
}
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
printf("%d ", ans[i][j] < inf ? 2 * ans[i][j] : -1);
}
printf("\n");
}
return 0;
}
Compilation message
sandwich.cpp: In function 'int main()':
sandwich.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &r, &c);
~~~~~^~~~~~~~~~~~~~~~
sandwich.cpp:52:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", grid[i]);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12152 KB |
Output is correct |
2 |
Correct |
13 ms |
12152 KB |
Output is correct |
3 |
Correct |
14 ms |
12848 KB |
Output is correct |
4 |
Correct |
13 ms |
12848 KB |
Output is correct |
5 |
Correct |
14 ms |
12848 KB |
Output is correct |
6 |
Correct |
34 ms |
13448 KB |
Output is correct |
7 |
Correct |
24 ms |
13448 KB |
Output is correct |
8 |
Correct |
23 ms |
13448 KB |
Output is correct |
9 |
Correct |
21 ms |
13448 KB |
Output is correct |
10 |
Correct |
24 ms |
13448 KB |
Output is correct |
11 |
Correct |
31 ms |
13448 KB |
Output is correct |
12 |
Correct |
19 ms |
13448 KB |
Output is correct |
13 |
Correct |
30 ms |
13448 KB |
Output is correct |
14 |
Correct |
28 ms |
13448 KB |
Output is correct |
15 |
Correct |
19 ms |
13448 KB |
Output is correct |
16 |
Correct |
23 ms |
13448 KB |
Output is correct |
17 |
Correct |
20 ms |
13448 KB |
Output is correct |
18 |
Correct |
22 ms |
13448 KB |
Output is correct |
19 |
Correct |
21 ms |
13448 KB |
Output is correct |
20 |
Correct |
19 ms |
13448 KB |
Output is correct |
21 |
Correct |
23 ms |
13448 KB |
Output is correct |
22 |
Correct |
22 ms |
13448 KB |
Output is correct |
23 |
Correct |
23 ms |
13448 KB |
Output is correct |
24 |
Correct |
29 ms |
13448 KB |
Output is correct |
25 |
Correct |
29 ms |
13448 KB |
Output is correct |
26 |
Correct |
26 ms |
13448 KB |
Output is correct |
27 |
Correct |
23 ms |
13448 KB |
Output is correct |
28 |
Correct |
22 ms |
13448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12152 KB |
Output is correct |
2 |
Correct |
13 ms |
12152 KB |
Output is correct |
3 |
Correct |
14 ms |
12848 KB |
Output is correct |
4 |
Correct |
13 ms |
12848 KB |
Output is correct |
5 |
Correct |
14 ms |
12848 KB |
Output is correct |
6 |
Correct |
34 ms |
13448 KB |
Output is correct |
7 |
Correct |
24 ms |
13448 KB |
Output is correct |
8 |
Correct |
23 ms |
13448 KB |
Output is correct |
9 |
Correct |
21 ms |
13448 KB |
Output is correct |
10 |
Correct |
24 ms |
13448 KB |
Output is correct |
11 |
Correct |
31 ms |
13448 KB |
Output is correct |
12 |
Correct |
19 ms |
13448 KB |
Output is correct |
13 |
Correct |
30 ms |
13448 KB |
Output is correct |
14 |
Correct |
28 ms |
13448 KB |
Output is correct |
15 |
Correct |
19 ms |
13448 KB |
Output is correct |
16 |
Correct |
23 ms |
13448 KB |
Output is correct |
17 |
Correct |
20 ms |
13448 KB |
Output is correct |
18 |
Correct |
22 ms |
13448 KB |
Output is correct |
19 |
Correct |
21 ms |
13448 KB |
Output is correct |
20 |
Correct |
19 ms |
13448 KB |
Output is correct |
21 |
Correct |
23 ms |
13448 KB |
Output is correct |
22 |
Correct |
22 ms |
13448 KB |
Output is correct |
23 |
Correct |
23 ms |
13448 KB |
Output is correct |
24 |
Correct |
29 ms |
13448 KB |
Output is correct |
25 |
Correct |
29 ms |
13448 KB |
Output is correct |
26 |
Correct |
26 ms |
13448 KB |
Output is correct |
27 |
Correct |
23 ms |
13448 KB |
Output is correct |
28 |
Correct |
22 ms |
13448 KB |
Output is correct |
29 |
Correct |
13 ms |
13448 KB |
Output is correct |
30 |
Correct |
26 ms |
16568 KB |
Output is correct |
31 |
Execution timed out |
8051 ms |
50748 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |