답안 #247472

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
247472 2020-07-11T12:51:05 Z arnold518 Sandwich (JOI16_sandwich) C++14
100 / 100
3137 ms 6864 KB
#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

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);
                      ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 512 KB Output is correct
6 Correct 9 ms 640 KB Output is correct
7 Correct 7 ms 512 KB Output is correct
8 Correct 8 ms 512 KB Output is correct
9 Correct 6 ms 512 KB Output is correct
10 Correct 8 ms 512 KB Output is correct
11 Correct 8 ms 512 KB Output is correct
12 Correct 7 ms 512 KB Output is correct
13 Correct 7 ms 512 KB Output is correct
14 Correct 7 ms 512 KB Output is correct
15 Correct 6 ms 512 KB Output is correct
16 Correct 6 ms 512 KB Output is correct
17 Correct 6 ms 512 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 6 ms 512 KB Output is correct
20 Correct 6 ms 512 KB Output is correct
21 Correct 6 ms 512 KB Output is correct
22 Correct 6 ms 512 KB Output is correct
23 Correct 6 ms 512 KB Output is correct
24 Correct 8 ms 512 KB Output is correct
25 Correct 8 ms 512 KB Output is correct
26 Correct 8 ms 512 KB Output is correct
27 Correct 7 ms 512 KB Output is correct
28 Correct 7 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 512 KB Output is correct
6 Correct 9 ms 640 KB Output is correct
7 Correct 7 ms 512 KB Output is correct
8 Correct 8 ms 512 KB Output is correct
9 Correct 6 ms 512 KB Output is correct
10 Correct 8 ms 512 KB Output is correct
11 Correct 8 ms 512 KB Output is correct
12 Correct 7 ms 512 KB Output is correct
13 Correct 7 ms 512 KB Output is correct
14 Correct 7 ms 512 KB Output is correct
15 Correct 6 ms 512 KB Output is correct
16 Correct 6 ms 512 KB Output is correct
17 Correct 6 ms 512 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 6 ms 512 KB Output is correct
20 Correct 6 ms 512 KB Output is correct
21 Correct 6 ms 512 KB Output is correct
22 Correct 6 ms 512 KB Output is correct
23 Correct 6 ms 512 KB Output is correct
24 Correct 8 ms 512 KB Output is correct
25 Correct 8 ms 512 KB Output is correct
26 Correct 8 ms 512 KB Output is correct
27 Correct 7 ms 512 KB Output is correct
28 Correct 7 ms 512 KB Output is correct
29 Correct 8 ms 384 KB Output is correct
30 Correct 5 ms 1792 KB Output is correct
31 Correct 3137 ms 6256 KB Output is correct
32 Correct 3099 ms 6276 KB Output is correct
33 Correct 239 ms 2680 KB Output is correct
34 Correct 715 ms 2936 KB Output is correct
35 Correct 623 ms 2424 KB Output is correct
36 Correct 6 ms 1792 KB Output is correct
37 Correct 897 ms 3032 KB Output is correct
38 Correct 826 ms 2908 KB Output is correct
39 Correct 871 ms 2936 KB Output is correct
40 Correct 1433 ms 3068 KB Output is correct
41 Correct 962 ms 2168 KB Output is correct
42 Correct 1239 ms 3448 KB Output is correct
43 Correct 1199 ms 3320 KB Output is correct
44 Correct 1224 ms 3252 KB Output is correct
45 Correct 678 ms 2900 KB Output is correct
46 Correct 494 ms 2808 KB Output is correct
47 Correct 576 ms 2896 KB Output is correct
48 Correct 360 ms 2552 KB Output is correct
49 Correct 522 ms 2808 KB Output is correct
50 Correct 559 ms 2808 KB Output is correct
51 Correct 514 ms 2936 KB Output is correct
52 Correct 516 ms 2936 KB Output is correct
53 Correct 511 ms 2808 KB Output is correct
54 Correct 478 ms 2884 KB Output is correct
55 Correct 531 ms 2936 KB Output is correct
56 Correct 510 ms 2936 KB Output is correct
57 Correct 358 ms 2680 KB Output is correct
58 Correct 1257 ms 3664 KB Output is correct
59 Correct 1299 ms 3584 KB Output is correct
60 Correct 895 ms 3064 KB Output is correct
61 Correct 1452 ms 6864 KB Output is correct
62 Correct 1190 ms 2936 KB Output is correct
63 Correct 1199 ms 3048 KB Output is correct