# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
247472 | arnold518 | Sandwich (JOI16_sandwich) | C++14 | 3137 ms | 6864 KiB |
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;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 400;
const int INF = 1e7;
int N, M;
char A[MAXN+10][MAXN+10];
int ans[MAXN+10][MAXN+10];
int vis[MAXN+10][MAXN+10];
int cnt;
void dfs(int y, int x)
{
cnt++;
if(A[y][x]=='N')
{
if(vis[y][x]==1)
{
if(vis[y-1][x]>0) cnt=-INF;
if(y!=1 && !vis[y-1][x])
{
vis[y-1][x]=1;
dfs(y-1, x);
}
if(vis[y][x+1]>0) cnt=-INF;
if(x!=M && !vis[y][x+1])
{
if(A[y][x+1]=='N') vis[y][x+1]=1;
else vis[y][x+1]=2;
dfs(y, x+1);
}
}
if(vis[y][x]==2)
{
if(vis[y+1][x]>0) cnt=-INF;
if(y!=N && !vis[y+1][x])
{
vis[y+1][x]=2;
dfs(y+1, x);
}
if(vis[y][x-1]>0) cnt=-INF;
if(x!=1 && !vis[y][x-1])
{
if(A[y][x-1]=='N') vis[y][x-1]=2;
else vis[y][x-1]=1;
dfs(y, x-1);
}
}
}
else
{
if(vis[y][x]==1)
{
if(vis[y-1][x]>0) cnt=-INF;
if(y!=1 && !vis[y-1][x])
{
vis[y-1][x]=1;
dfs(y-1, x);
}
if(vis[y][x-1]>0) cnt=-INF;
if(x!=1 && !vis[y][x-1])
{
if(A[y][x-1]=='N') vis[y][x-1]=2;
else vis[y][x-1]=1;
dfs(y, x-1);
}
}
if(vis[y][x]==2)
{
if(vis[y+1][x]>0) cnt=-INF;
if(y!=N && !vis[y+1][x])
{
vis[y+1][x]=2;
dfs(y+1, x);
}
if(vis[y][x+1]>0) cnt=-INF;
if(x!=M && !vis[y][x+1])
{
if(A[y][x+1]=='N') vis[y][x+1]=1;
else vis[y][x+1]=2;
dfs(y, x+1);
}
}
}
vis[y][x]=-1;
}
int main()
{
int i, j, p, q;
scanf("%d%d", &N, &M);
for(i=1; i<=N; i++) scanf(" %s", A[i]+1);
for(i=1; i<=N; i++) for(j=1; j<=M; j++) ans[i][j]=INF;
for(j=1; j<=M; j++)
{
for(p=1; p<=N; p++) for(q=1; q<=M; q++) vis[p][q]=0; cnt=0;
for(i=1; i<=N; i++)
{
if(!vis[i][j])
{
vis[i][j]=1;
dfs(i, j);
}
if(cnt>=0) ans[i][j]=min(ans[i][j], cnt);
}
}
for(j=1; j<=M; j++)
{
for(p=1; p<=N; p++) for(q=1; q<=M; q++) vis[p][q]=0; cnt=0;
for(i=N; i>=1; i--)
{
if(!vis[i][j])
{
vis[i][j]=2;
dfs(i, j);
}
if(cnt>=0) ans[i][j]=min(ans[i][j], cnt);
}
}
for(i=1; i<=N; i++)
{
for(j=1; j<=M; j++)
{
if(ans[i][j]==INF) printf("-1 ");
else printf("%d ", ans[i][j]*2);
}
printf("\n");
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |