#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;
};
_edge edge[2][501][501][2];
int used[2][500][500];
int fin[2][500][500];
int imin(int x, int y) {
return x < y ? x : y;
}
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 (int i = 0; i < 2; ++i) {
if (edge[t][x][y][i].t == -1) continue;
_edge j = edge[t][x][y][i];
ret += dfs(j.t, j.x, j.y);
if (ret > inf) break;
}
fin[t][x][y] = 1;
return imin(ret, inf);
}
int ans[500][500];
char buf[10000];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> r >> c;
for (int i = 0; i < r; ++i) {
cin >> grid[i];
}
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
edge[0][i][j][0] = edge[0][i][j][1] = edge[1][i][j][0] = edge[1][i][j][1] = { -1, -1, -1 };
if (j) edge[0][i][j][0] = { 0, i, j - 1 };
if (i + 1 < r) edge[grid[i][j] != 'N'][i][j][1] = { grid[i + 1][j] != 'N', i + 1, j };
if (j + 1 < c) edge[1][i][j][0] = { 1, i, j + 1 };
if (i) edge[grid[i][j] != 'Z'][i][j][1] = { 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) {
if (sum < inf) sum += dfs(0, i, j);
sum = imin(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--; ) {
if (sum < inf) sum += dfs(1, i, j);
sum = imin(sum, inf);
ans[i][j] = imin(ans[i][j], sum);
}
}
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
cout << (ans[i][j] < inf ? 2 * ans[i][j] : -1);
cout << (j + 1 < c ? ' ' : '\n');
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
1308 KB |
Output is correct |
4 |
Correct |
2 ms |
1308 KB |
Output is correct |
5 |
Correct |
2 ms |
1376 KB |
Output is correct |
6 |
Correct |
5 ms |
1560 KB |
Output is correct |
7 |
Correct |
9 ms |
1592 KB |
Output is correct |
8 |
Correct |
8 ms |
1612 KB |
Output is correct |
9 |
Correct |
7 ms |
1612 KB |
Output is correct |
10 |
Correct |
11 ms |
1616 KB |
Output is correct |
11 |
Correct |
14 ms |
1628 KB |
Output is correct |
12 |
Correct |
7 ms |
1628 KB |
Output is correct |
13 |
Correct |
14 ms |
1664 KB |
Output is correct |
14 |
Correct |
14 ms |
1788 KB |
Output is correct |
15 |
Correct |
7 ms |
1788 KB |
Output is correct |
16 |
Correct |
9 ms |
1820 KB |
Output is correct |
17 |
Correct |
8 ms |
1820 KB |
Output is correct |
18 |
Correct |
9 ms |
1820 KB |
Output is correct |
19 |
Correct |
9 ms |
1820 KB |
Output is correct |
20 |
Correct |
5 ms |
1820 KB |
Output is correct |
21 |
Correct |
6 ms |
1820 KB |
Output is correct |
22 |
Correct |
6 ms |
1820 KB |
Output is correct |
23 |
Correct |
6 ms |
1820 KB |
Output is correct |
24 |
Correct |
12 ms |
1820 KB |
Output is correct |
25 |
Correct |
12 ms |
1820 KB |
Output is correct |
26 |
Correct |
10 ms |
1836 KB |
Output is correct |
27 |
Correct |
6 ms |
1964 KB |
Output is correct |
28 |
Correct |
9 ms |
2108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
1308 KB |
Output is correct |
4 |
Correct |
2 ms |
1308 KB |
Output is correct |
5 |
Correct |
2 ms |
1376 KB |
Output is correct |
6 |
Correct |
5 ms |
1560 KB |
Output is correct |
7 |
Correct |
9 ms |
1592 KB |
Output is correct |
8 |
Correct |
8 ms |
1612 KB |
Output is correct |
9 |
Correct |
7 ms |
1612 KB |
Output is correct |
10 |
Correct |
11 ms |
1616 KB |
Output is correct |
11 |
Correct |
14 ms |
1628 KB |
Output is correct |
12 |
Correct |
7 ms |
1628 KB |
Output is correct |
13 |
Correct |
14 ms |
1664 KB |
Output is correct |
14 |
Correct |
14 ms |
1788 KB |
Output is correct |
15 |
Correct |
7 ms |
1788 KB |
Output is correct |
16 |
Correct |
9 ms |
1820 KB |
Output is correct |
17 |
Correct |
8 ms |
1820 KB |
Output is correct |
18 |
Correct |
9 ms |
1820 KB |
Output is correct |
19 |
Correct |
9 ms |
1820 KB |
Output is correct |
20 |
Correct |
5 ms |
1820 KB |
Output is correct |
21 |
Correct |
6 ms |
1820 KB |
Output is correct |
22 |
Correct |
6 ms |
1820 KB |
Output is correct |
23 |
Correct |
6 ms |
1820 KB |
Output is correct |
24 |
Correct |
12 ms |
1820 KB |
Output is correct |
25 |
Correct |
12 ms |
1820 KB |
Output is correct |
26 |
Correct |
10 ms |
1836 KB |
Output is correct |
27 |
Correct |
6 ms |
1964 KB |
Output is correct |
28 |
Correct |
9 ms |
2108 KB |
Output is correct |
29 |
Correct |
2 ms |
2108 KB |
Output is correct |
30 |
Correct |
17 ms |
7996 KB |
Output is correct |
31 |
Correct |
408 ms |
14988 KB |
Output is correct |
32 |
Correct |
441 ms |
15244 KB |
Output is correct |
33 |
Correct |
102 ms |
15244 KB |
Output is correct |
34 |
Correct |
3438 ms |
15324 KB |
Output is correct |
35 |
Correct |
2685 ms |
15324 KB |
Output is correct |
36 |
Correct |
68 ms |
15324 KB |
Output is correct |
37 |
Correct |
4998 ms |
15324 KB |
Output is correct |
38 |
Correct |
4455 ms |
15324 KB |
Output is correct |
39 |
Correct |
5195 ms |
15324 KB |
Output is correct |
40 |
Execution timed out |
8005 ms |
15324 KB |
Time limit exceeded |
41 |
Halted |
0 ms |
0 KB |
- |