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>
//#pragma GCC optimize("Ofast","unroll-loops")
//#pragma GCC target("popcnt")
using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL;
const ll M = 1000LL * 1000LL * 1000LL + 7LL;
const int N = 400 + 7;
int n, m;
int vis[N][N];
int il = 0;
string s[N];
int answer[N][N];
queue<int> q;
inline void Clean()
{
il = 0;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
vis[i][j] = 0;
}
bool DFS(int r, int i, int j)
{
if(vis[i][j])
return vis[i][j] == 2;
vis[i][j] = 2;
++il; ++il;
if(r == 0)
{
if(i > 1 && DFS(0, i - 1, j)) return true;
if(j > 1 && s[i][j] == 'Z')
if(DFS((s[i][j - 1] != 'Z'), i, j - 1))
return true;
if(j < m && s[i][j] == 'N' && DFS((s[i][j + 1] != 'N'), i, j + 1))
return true;
}else
{
if(i < n && DFS(1, i + 1, j)) return true;
if(j > 1 && s[i][j] == 'N' && DFS((s[i][j - 1] != 'Z'), i, j - 1))
return true;
if(j < m && s[i][j] == 'Z' && DFS((s[i][j + 1] != 'N'), i, j + 1))
return true;
}
vis[i][j] = 1;
return false;
}
void Solve()
{
cin >> n >> m;
for(int i = 1; i <= n; ++i)
{
cin >> s[i];
s[i] = '#' + s[i];
for(int j = 1; j <= m; ++j)
answer[i][j] = N * N * 4;
}
for(int j = 1; j <= m; ++j)
{
for(int i = 1; i <= n; ++i)
{
if(DFS(0, i, j))
break;
answer[i][j] = min(answer[i][j], il);
}
Clean();
for(int i = n; i >= 1; --i)
{
if(DFS(1, i, j))
break;
answer[i][j] = min(answer[i][j], il);
}
Clean();
}
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= m; ++j)
{
if(answer[i][j] == N * N * 4) answer[i][j] = -1;
cout << answer[i][j] << " ";
}
cout << "\n";
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//int t; cin >> t;
//while(t--)
Solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |