제출 #247472

#제출 시각아이디문제언어결과실행 시간메모리
247472arnold518Sandwich (JOI16_sandwich)C++14
100 / 100
3137 ms6864 KiB
#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");
	}
}

컴파일 시 표준 에러 (stderr) 메시지

sandwich.cpp: In function 'int main()':
sandwich.cpp:105:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   for(p=1; p<=N; p++) for(q=1; q<=M; q++) vis[p][q]=0; cnt=0;
   ^~~
sandwich.cpp:105:56: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   for(p=1; p<=N; p++) for(q=1; q<=M; q++) vis[p][q]=0; cnt=0;
                                                        ^~~
sandwich.cpp:119:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   for(p=1; p<=N; p++) for(q=1; q<=M; q++) vis[p][q]=0; cnt=0;
   ^~~
sandwich.cpp:119:56: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   for(p=1; p<=N; p++) for(q=1; q<=M; q++) vis[p][q]=0; cnt=0;
                                                        ^~~
sandwich.cpp:98:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &M);
  ~~~~~^~~~~~~~~~~~~~~~
sandwich.cpp:99:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=1; i<=N; i++) scanf(" %s", A[i]+1);
                      ~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...