#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, M;
cin >> N >> M;
vector<string> a(N);
for(int i = 0; i < N; ++i){
cin >> a[i];
}
vector<vector<int>> chk(N, vector<int>(M, 0));
vector<vector<int>> ans(N, vector<int>(M, (int)1e9));
vector<int> NN = {0, 0, 1, 1}, ZZ = {0, 1, 1, 0};
vector<int> wx = {-1, 0, 1, 0}, wy = {0, 1, 0, -1};
function<int(int, int, int)> dfs = [&](int x, int y, int dir){
if(chk[x][y] == 1){
return (int)1e9;
}
if(chk[x][y] == 2){
return 0;
}
int rv = 1;
chk[x][y] = 1;
for(int i = 0; i < 4; ++i){
if((a[x][y] == 'N' && NN[i] == dir) || (a[x][y] == 'Z' && ZZ[i] == dir)){
int nx = x + wx[i], ny = y + wy[i];
if(nx >= 0 && ny >= 0 && nx < N && ny < M){
if(a[nx][ny] == 'N'){
rv += dfs(nx, ny, NN[i]);
if(rv >= (int)1e9) return rv;
}
else{
rv += dfs(nx, ny, ZZ[i]);
if(rv >= (int)1e9) return rv;
}
}
}
}
chk[x][y] = 2;
return rv;
};
for(int j = 0; j < M; ++j){
chk = vector<vector<int>>(N, vector<int>(M, 0));
int sum = 0;
for(int i = 0; i < N; ++i){
if(chk[i][j]) break;
sum += dfs(i, j, 0);
if(sum > (int)1e8){
break;
}
ans[i][j] = min(sum * 2, ans[i][j]);
}
chk = vector<vector<int>>(N, vector<int>(M, 0));
sum = 0;
for(int i = N - 1; i >= 0; --i){
if(chk[i][j]) break;
sum += dfs(i, j, 1);
if(sum > (int)1e8){
break;
}
ans[i][j] = min(sum * 2, ans[i][j]);
}
}
for(int i = 0; i < N; ++i){
for(int j = 0; j < M; ++j){
if(ans[i][j] == (int)1e9){
ans[i][j] = -1;
}
cout << ans[i][j] << ' ';
}
cout << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
7 ms |
364 KB |
Output is correct |
8 |
Correct |
8 ms |
364 KB |
Output is correct |
9 |
Correct |
5 ms |
364 KB |
Output is correct |
10 |
Correct |
10 ms |
364 KB |
Output is correct |
11 |
Correct |
9 ms |
364 KB |
Output is correct |
12 |
Correct |
5 ms |
364 KB |
Output is correct |
13 |
Correct |
8 ms |
492 KB |
Output is correct |
14 |
Correct |
9 ms |
492 KB |
Output is correct |
15 |
Correct |
6 ms |
364 KB |
Output is correct |
16 |
Correct |
5 ms |
364 KB |
Output is correct |
17 |
Correct |
5 ms |
364 KB |
Output is correct |
18 |
Correct |
5 ms |
364 KB |
Output is correct |
19 |
Correct |
6 ms |
364 KB |
Output is correct |
20 |
Correct |
4 ms |
364 KB |
Output is correct |
21 |
Correct |
4 ms |
364 KB |
Output is correct |
22 |
Correct |
4 ms |
364 KB |
Output is correct |
23 |
Correct |
5 ms |
364 KB |
Output is correct |
24 |
Correct |
9 ms |
364 KB |
Output is correct |
25 |
Correct |
9 ms |
364 KB |
Output is correct |
26 |
Correct |
8 ms |
364 KB |
Output is correct |
27 |
Correct |
11 ms |
364 KB |
Output is correct |
28 |
Correct |
12 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
7 ms |
364 KB |
Output is correct |
8 |
Correct |
8 ms |
364 KB |
Output is correct |
9 |
Correct |
5 ms |
364 KB |
Output is correct |
10 |
Correct |
10 ms |
364 KB |
Output is correct |
11 |
Correct |
9 ms |
364 KB |
Output is correct |
12 |
Correct |
5 ms |
364 KB |
Output is correct |
13 |
Correct |
8 ms |
492 KB |
Output is correct |
14 |
Correct |
9 ms |
492 KB |
Output is correct |
15 |
Correct |
6 ms |
364 KB |
Output is correct |
16 |
Correct |
5 ms |
364 KB |
Output is correct |
17 |
Correct |
5 ms |
364 KB |
Output is correct |
18 |
Correct |
5 ms |
364 KB |
Output is correct |
19 |
Correct |
6 ms |
364 KB |
Output is correct |
20 |
Correct |
4 ms |
364 KB |
Output is correct |
21 |
Correct |
4 ms |
364 KB |
Output is correct |
22 |
Correct |
4 ms |
364 KB |
Output is correct |
23 |
Correct |
5 ms |
364 KB |
Output is correct |
24 |
Correct |
9 ms |
364 KB |
Output is correct |
25 |
Correct |
9 ms |
364 KB |
Output is correct |
26 |
Correct |
8 ms |
364 KB |
Output is correct |
27 |
Correct |
11 ms |
364 KB |
Output is correct |
28 |
Correct |
12 ms |
492 KB |
Output is correct |
29 |
Correct |
9 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
158 ms |
3308 KB |
Output is correct |
32 |
Correct |
147 ms |
3120 KB |
Output is correct |
33 |
Correct |
17 ms |
1256 KB |
Output is correct |
34 |
Correct |
2984 ms |
3320 KB |
Output is correct |
35 |
Correct |
2334 ms |
2756 KB |
Output is correct |
36 |
Correct |
3 ms |
492 KB |
Output is correct |
37 |
Correct |
3602 ms |
3496 KB |
Output is correct |
38 |
Correct |
3382 ms |
3392 KB |
Output is correct |
39 |
Correct |
3507 ms |
3160 KB |
Output is correct |
40 |
Correct |
4372 ms |
3572 KB |
Output is correct |
41 |
Correct |
2820 ms |
2484 KB |
Output is correct |
42 |
Correct |
4215 ms |
3932 KB |
Output is correct |
43 |
Correct |
4140 ms |
4068 KB |
Output is correct |
44 |
Correct |
4078 ms |
3700 KB |
Output is correct |
45 |
Correct |
2982 ms |
3272 KB |
Output is correct |
46 |
Correct |
2081 ms |
3156 KB |
Output is correct |
47 |
Correct |
2410 ms |
3072 KB |
Output is correct |
48 |
Correct |
1351 ms |
2460 KB |
Output is correct |
49 |
Correct |
2361 ms |
3108 KB |
Output is correct |
50 |
Correct |
2430 ms |
3348 KB |
Output is correct |
51 |
Correct |
2097 ms |
3100 KB |
Output is correct |
52 |
Correct |
2045 ms |
3192 KB |
Output is correct |
53 |
Correct |
1824 ms |
3384 KB |
Output is correct |
54 |
Correct |
1794 ms |
3276 KB |
Output is correct |
55 |
Correct |
2088 ms |
3296 KB |
Output is correct |
56 |
Correct |
2073 ms |
3092 KB |
Output is correct |
57 |
Correct |
1344 ms |
2604 KB |
Output is correct |
58 |
Correct |
4524 ms |
3504 KB |
Output is correct |
59 |
Correct |
4683 ms |
3764 KB |
Output is correct |
60 |
Correct |
2724 ms |
2796 KB |
Output is correct |
61 |
Correct |
4090 ms |
3552 KB |
Output is correct |
62 |
Correct |
5802 ms |
3300 KB |
Output is correct |
63 |
Correct |
5593 ms |
4064 KB |
Output is correct |