#include"bits/stdc++.h"
using namespace std;
#define LL long long
#define REP(i, n) for(int (i)=0; (i)<(n); (i)++)
#define PB push_back
#define MP make_pair
#define all(x) x.begin(),x.end()
int H,W;
string S[400];
int state[400][400]; //0:初期、1:計算中、2:計算終了
/*
3
1 2
0
*/
int dfs(int y, int x, int d){
if(x < 0 || x >= W || y < 0 || y >= H) return 0;
if(state[y][x] == 1) return INT_MAX;
if(state[y][x] == 2) return 0;
state[y][x] = 1;
int ans = 1;
if(S[y][x] == 'N'){
if(d == 0 || d == 1){
int tmp = dfs(y, x+1, 1);
if(tmp == INT_MAX) return INT_MAX;
ans += tmp;
tmp = dfs(y-1, x, 0);
if(tmp == INT_MAX) return INT_MAX;
ans += tmp;
}
else{
int tmp = dfs(y, x-1, 2);
if(tmp == INT_MAX) return INT_MAX;
ans += tmp;
tmp = dfs(y+1, x, 3);
if(tmp == INT_MAX) return INT_MAX;
ans += tmp;
}
}
if(S[y][x] == 'Z'){
if(d == 0 || d == 2){
int tmp = dfs(y, x-1, 2);
if(tmp == INT_MAX) return INT_MAX;
ans += tmp;
tmp = dfs(y-1, x, 0);
if(tmp == INT_MAX) return INT_MAX;
ans += tmp;
}
else{
int tmp = dfs(y, x+1, 1);
if(tmp == INT_MAX) return INT_MAX;
ans += tmp;
tmp = dfs(y+1, x, 3);
if(tmp == INT_MAX) return INT_MAX;
ans += tmp;
}
}
state[y][x] = 2;
return ans;
}
int ans[400][400];
int main(){
cin >> H >> W;
for(int i=0; i<H; i++) cin >> S[i];
for(int i=0; i<H; i++){
for(int j=0; j<W; j++){
ans[i][j] = INT_MAX;
}
}
//右から入る
for(int i=0; i<H; i++){
for(int k=0; k<H; k++){
for(int l=0; l<W; l++){
state[k][l] = 0;
}
}
int tmp = 0;
for(int j=0; j<W; j++){
int tmp2 = dfs(i, j, 2);
if(tmp2 == INT_MAX || tmp == INT_MAX) tmp = INT_MAX;
else tmp += tmp2;
ans[i][j] = min(ans[i][j], tmp);
}
}
//左から入る
for(int i=0; i<H; i++){
for(int k=0; k<H; k++){
for(int l=0; l<W; l++){
state[k][l] = 0;
}
}
int tmp = 0;
for(int j=W-1; j>=0; j--){
int tmp2 = dfs(i, j, 1);
if(tmp2 == INT_MAX || tmp == INT_MAX) tmp = INT_MAX;
else tmp += tmp2;
ans[i][j] = min(ans[i][j], tmp);
}
}
for(int i=0; i<H; i++){
for(int j=0; j<W; j++){
if(j != 0) cout << " ";
if(ans[i][j] == INT_MAX) cout << -1;
else cout << ans[i][j]*2;
}
cout << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
560 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
4 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
7 ms |
512 KB |
Output is correct |
14 |
Correct |
7 ms |
512 KB |
Output is correct |
15 |
Correct |
4 ms |
512 KB |
Output is correct |
16 |
Correct |
4 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
544 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
19 |
Correct |
6 ms |
512 KB |
Output is correct |
20 |
Correct |
5 ms |
512 KB |
Output is correct |
21 |
Correct |
6 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
4 ms |
512 KB |
Output is correct |
24 |
Correct |
6 ms |
512 KB |
Output is correct |
25 |
Correct |
6 ms |
512 KB |
Output is correct |
26 |
Correct |
5 ms |
512 KB |
Output is correct |
27 |
Correct |
5 ms |
640 KB |
Output is correct |
28 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
560 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
4 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
7 ms |
512 KB |
Output is correct |
14 |
Correct |
7 ms |
512 KB |
Output is correct |
15 |
Correct |
4 ms |
512 KB |
Output is correct |
16 |
Correct |
4 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
544 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
19 |
Correct |
6 ms |
512 KB |
Output is correct |
20 |
Correct |
5 ms |
512 KB |
Output is correct |
21 |
Correct |
6 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
4 ms |
512 KB |
Output is correct |
24 |
Correct |
6 ms |
512 KB |
Output is correct |
25 |
Correct |
6 ms |
512 KB |
Output is correct |
26 |
Correct |
5 ms |
512 KB |
Output is correct |
27 |
Correct |
5 ms |
640 KB |
Output is correct |
28 |
Correct |
5 ms |
640 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
8 ms |
1664 KB |
Output is correct |
31 |
Correct |
98 ms |
2684 KB |
Output is correct |
32 |
Correct |
116 ms |
2680 KB |
Output is correct |
33 |
Correct |
29 ms |
1656 KB |
Output is correct |
34 |
Correct |
1111 ms |
2912 KB |
Output is correct |
35 |
Correct |
682 ms |
2420 KB |
Output is correct |
36 |
Correct |
34 ms |
1656 KB |
Output is correct |
37 |
Correct |
1038 ms |
2936 KB |
Output is correct |
38 |
Correct |
687 ms |
2904 KB |
Output is correct |
39 |
Correct |
987 ms |
2824 KB |
Output is correct |
40 |
Correct |
1791 ms |
3124 KB |
Output is correct |
41 |
Correct |
610 ms |
2244 KB |
Output is correct |
42 |
Correct |
1708 ms |
3312 KB |
Output is correct |
43 |
Correct |
1829 ms |
3404 KB |
Output is correct |
44 |
Correct |
1787 ms |
3320 KB |
Output is correct |
45 |
Correct |
1119 ms |
2908 KB |
Output is correct |
46 |
Correct |
802 ms |
2904 KB |
Output is correct |
47 |
Correct |
1030 ms |
2920 KB |
Output is correct |
48 |
Correct |
569 ms |
2560 KB |
Output is correct |
49 |
Correct |
757 ms |
2900 KB |
Output is correct |
50 |
Correct |
880 ms |
2912 KB |
Output is correct |
51 |
Correct |
593 ms |
2908 KB |
Output is correct |
52 |
Correct |
646 ms |
2904 KB |
Output is correct |
53 |
Correct |
608 ms |
2904 KB |
Output is correct |
54 |
Correct |
690 ms |
2772 KB |
Output is correct |
55 |
Correct |
673 ms |
2908 KB |
Output is correct |
56 |
Correct |
720 ms |
2808 KB |
Output is correct |
57 |
Correct |
673 ms |
2588 KB |
Output is correct |
58 |
Correct |
1575 ms |
3148 KB |
Output is correct |
59 |
Correct |
1714 ms |
3164 KB |
Output is correct |
60 |
Correct |
1034 ms |
2672 KB |
Output is correct |
61 |
Correct |
1611 ms |
3116 KB |
Output is correct |
62 |
Correct |
2070 ms |
15484 KB |
Output is correct |
63 |
Correct |
1758 ms |
15496 KB |
Output is correct |