#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#define rep(i,n) for (int i = 0; i < n; ++i)
using namespace std;
typedef vector<int> VI;
typedef vector<VI > VVI;
const int INF = 1001001001;
const int di[] = {-1,0,1,0}, dj[] = {0,-1,0,1}; //^<v>
int h, w;
VVI used, dv;
int dfs(int i, int j, int v) {
if (used[i][j] == 1) return INF;
if (used[i][j] == 2) return 0;
int res = 2;
used[i][j] = 1;
rep(k,2) {
int nv = v^(dv[i][j]*k);
int ni = i + di[nv], nj = j + dj[nv];
if (ni < 0 || nj < 0 || ni >= h || nj >= w) continue;
int r = dfs(ni,nj,nv);
if (r == INF) return INF;
res += r;
}
used[i][j] = 2;
return res;
}
int main() {
cin >> h >> w;
vector<string> s(h);
rep(i,h) cin >> s[i];
dv = VVI(h, VI(w));
rep(i,h)rep(j,w) dv[i][j] = (s[i][j] == 'Z') ? 1 : 3;
VVI ans(h, VI(w, INF));
rep(j,w)for (int v = 0; v <= 2; v += 2) {
used = VVI(h, VI(w));
int sum = 0;
rep(i,h) {
int si = i;
if (v == 2) si = h-1-i;
int res = dfs(si,j,v);
if (res == INF) break;
sum += res;
ans[si][j] = min(ans[si][j], sum);
}
}
rep(i,h)rep(j,w) if (ans[i][j] == INF) ans[i][j] = -1;
rep(i,h)rep(j,w) printf("%d%c", ans[i][j], (j == w-1) ? '\n' : ' ');
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
408 KB |
Output is correct |
4 |
Correct |
2 ms |
408 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
544 KB |
Output is correct |
7 |
Correct |
7 ms |
560 KB |
Output is correct |
8 |
Correct |
7 ms |
576 KB |
Output is correct |
9 |
Correct |
5 ms |
576 KB |
Output is correct |
10 |
Correct |
7 ms |
576 KB |
Output is correct |
11 |
Correct |
7 ms |
648 KB |
Output is correct |
12 |
Correct |
5 ms |
648 KB |
Output is correct |
13 |
Correct |
7 ms |
648 KB |
Output is correct |
14 |
Correct |
7 ms |
716 KB |
Output is correct |
15 |
Correct |
5 ms |
732 KB |
Output is correct |
16 |
Correct |
5 ms |
732 KB |
Output is correct |
17 |
Correct |
4 ms |
732 KB |
Output is correct |
18 |
Correct |
5 ms |
732 KB |
Output is correct |
19 |
Correct |
7 ms |
732 KB |
Output is correct |
20 |
Correct |
5 ms |
732 KB |
Output is correct |
21 |
Correct |
4 ms |
732 KB |
Output is correct |
22 |
Correct |
5 ms |
732 KB |
Output is correct |
23 |
Correct |
5 ms |
732 KB |
Output is correct |
24 |
Correct |
8 ms |
732 KB |
Output is correct |
25 |
Correct |
9 ms |
732 KB |
Output is correct |
26 |
Correct |
7 ms |
804 KB |
Output is correct |
27 |
Correct |
9 ms |
804 KB |
Output is correct |
28 |
Correct |
10 ms |
804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
408 KB |
Output is correct |
4 |
Correct |
2 ms |
408 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
544 KB |
Output is correct |
7 |
Correct |
7 ms |
560 KB |
Output is correct |
8 |
Correct |
7 ms |
576 KB |
Output is correct |
9 |
Correct |
5 ms |
576 KB |
Output is correct |
10 |
Correct |
7 ms |
576 KB |
Output is correct |
11 |
Correct |
7 ms |
648 KB |
Output is correct |
12 |
Correct |
5 ms |
648 KB |
Output is correct |
13 |
Correct |
7 ms |
648 KB |
Output is correct |
14 |
Correct |
7 ms |
716 KB |
Output is correct |
15 |
Correct |
5 ms |
732 KB |
Output is correct |
16 |
Correct |
5 ms |
732 KB |
Output is correct |
17 |
Correct |
4 ms |
732 KB |
Output is correct |
18 |
Correct |
5 ms |
732 KB |
Output is correct |
19 |
Correct |
7 ms |
732 KB |
Output is correct |
20 |
Correct |
5 ms |
732 KB |
Output is correct |
21 |
Correct |
4 ms |
732 KB |
Output is correct |
22 |
Correct |
5 ms |
732 KB |
Output is correct |
23 |
Correct |
5 ms |
732 KB |
Output is correct |
24 |
Correct |
8 ms |
732 KB |
Output is correct |
25 |
Correct |
9 ms |
732 KB |
Output is correct |
26 |
Correct |
7 ms |
804 KB |
Output is correct |
27 |
Correct |
9 ms |
804 KB |
Output is correct |
28 |
Correct |
10 ms |
804 KB |
Output is correct |
29 |
Correct |
7 ms |
804 KB |
Output is correct |
30 |
Correct |
2 ms |
804 KB |
Output is correct |
31 |
Correct |
213 ms |
4116 KB |
Output is correct |
32 |
Correct |
188 ms |
4116 KB |
Output is correct |
33 |
Correct |
26 ms |
4116 KB |
Output is correct |
34 |
Correct |
1683 ms |
4312 KB |
Output is correct |
35 |
Correct |
1739 ms |
4312 KB |
Output is correct |
36 |
Correct |
6 ms |
4312 KB |
Output is correct |
37 |
Correct |
2305 ms |
4680 KB |
Output is correct |
38 |
Correct |
1929 ms |
4868 KB |
Output is correct |
39 |
Correct |
2295 ms |
4972 KB |
Output is correct |
40 |
Correct |
3329 ms |
5452 KB |
Output is correct |
41 |
Correct |
1868 ms |
5452 KB |
Output is correct |
42 |
Correct |
2870 ms |
5664 KB |
Output is correct |
43 |
Correct |
3071 ms |
5820 KB |
Output is correct |
44 |
Correct |
2722 ms |
5888 KB |
Output is correct |
45 |
Correct |
1724 ms |
5888 KB |
Output is correct |
46 |
Correct |
1366 ms |
6096 KB |
Output is correct |
47 |
Correct |
1481 ms |
6096 KB |
Output is correct |
48 |
Correct |
877 ms |
6096 KB |
Output is correct |
49 |
Correct |
1209 ms |
6476 KB |
Output is correct |
50 |
Correct |
1492 ms |
6532 KB |
Output is correct |
51 |
Correct |
1374 ms |
6668 KB |
Output is correct |
52 |
Correct |
1403 ms |
6832 KB |
Output is correct |
53 |
Correct |
1293 ms |
7100 KB |
Output is correct |
54 |
Correct |
1206 ms |
7136 KB |
Output is correct |
55 |
Correct |
1267 ms |
7308 KB |
Output is correct |
56 |
Correct |
1265 ms |
7464 KB |
Output is correct |
57 |
Correct |
954 ms |
7464 KB |
Output is correct |
58 |
Correct |
2634 ms |
8104 KB |
Output is correct |
59 |
Correct |
3016 ms |
8296 KB |
Output is correct |
60 |
Correct |
1690 ms |
8296 KB |
Output is correct |
61 |
Correct |
2767 ms |
8588 KB |
Output is correct |
62 |
Correct |
3121 ms |
8628 KB |
Output is correct |
63 |
Correct |
3234 ms |
9096 KB |
Output is correct |