This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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]);
rv = min(rv, (int)1e9);
}
else{
rv += dfs(nx, ny, ZZ[i]);
rv = min(rv, (int)1e9);
}
}
}
}
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){
sum += dfs(i, j, 0);
if(sum > (int)1e9){
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){
sum += dfs(i, j, 1);
if(sum > (int)1e9){
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |