답안 #22501

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
22501 2017-04-30T05:07:15 Z 현우야 이거 플로우야(#903, waylight3, pinebananais, ae04071) Young Zebra (KRIII5_YZ) C++11
2 / 7
103 ms 8036 KB
#include <stdio.h>
#include <queue>
#include <algorithm>
#include <utility>
#include <cstring>

typedef std::pair<int, int> ip;
const int dy[4]={0,1,0,-1}, dx[4]={1,0,-1,0};
int n,m,cost[500][500][4],vis[500][500],res[500][500];
char str[500][500];

int gety(int y, int d) {
	y+=dy[d];
	if(y<0) y+=n;
	else if(y>=n) y-=n;
	return y;
}
int getx(int x, int d) {
	x+=dx[d];
	if(x<0) x+=m;
	else if(x>=m) x-=m;
	return x;
}
bool Check(int sy, int sx, int di) {
	int len;
	if(di==0||di==2) len=m;
	else len=n;

	std::queue<ip> que;
	que.push(ip(sy,sx));
	cost[sy][sx][di]=0;
	while(!que.empty()) {
		ip now=que.front();que.pop();
		for(int k=0;k<4;k++) {
			if((k+2)%4==di) continue;

			int cy=gety(now.first,k), cx=getx(now.second,k);
			if(str[cy][cx]==str[sy][sx]) {
				// printf("%d %d %d %d\n",now.first,now.second,cy,cx);
				if(cost[cy][cx][di]==-1) {					
					cost[cy][cx][di]=cost[now.first][now.second][di]+(di==k);
					que.push(ip(cy,cx));
				}
				else if(di==k) {
					if(cost[now.first][now.second][di]-cost[cy][cx][di]+1==len) {
						// printf("%d %d %d %d\n",now.first,now.second,cy,cx);
						return true;
					}
				}
			}
		}
	}
	return false;
}

void BFS(int sy, int sx, bool chk) {
	std::queue<ip> que;
	que.push(ip(sy,sx));
	if(chk) res[sy][sx]=-1;
	vis[sy][sx]=1;

	int sz=1;
	while(!que.empty()) {
		ip now=que.front();que.pop();

		for(int k=0;k<4;k++) {
			int cy=gety(now.first,k), cx=getx(now.second,k);
			if(vis[cy][cx]==0&&str[cy][cx]==str[sy][sx]) {
				que.push(ip(cy,cx));
				vis[cy][cx]=1;
				if(chk) res[cy][cx]=-1;
				++sz;
			}
		}
	}
	if(chk) return;

	que.push(ip(sy,sx));
	res[sy][sx]=sz;
	vis[sy][sx]=2;
	while(!que.empty()) {
		ip now=que.front();que.pop();
		for(int k=0;k<4;k++) {
			int cy=gety(now.first,k), cx=getx(now.second,k);
			if(str[cy][cx]==str[sy][sx]&&vis[cy][cx]==1) {
				que.push(ip(cy,cx));
				vis[cy][cx]=2;
				res[cy][cx]=sz;
			}
		}
	}
}

int main() {
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++)
		scanf("%s",str[i]);

	memset(cost, -1, sizeof(cost));
	for(int i=0;i<n;i++) {
		for(int j=0;j<m;j++) {
			if(vis[i][j]==0) {
				bool flag=false;
				for(int k=0;k<4;k++) {
					flag=Check(i,j,k);
					if(flag) break;
				}
				BFS(i,j,flag);
				// puts("");
			}
		}
	}

	for(int i=0;i<n;i++) {
		for(int j=0;j<m;j++)
			printf("%d ",res[i][j]);
		puts("");
	}

	return 0;
}

Compilation message

YZ.cpp: In function 'int main()':
YZ.cpp:95:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&m);
                      ^
YZ.cpp:97:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s",str[i]);
                     ^
# 결과 실행 시간 메모리 Grader output
1 Partially correct 33 ms 8036 KB Output is partially correct
2 Correct 23 ms 8036 KB Output is correct
3 Partially correct 43 ms 8036 KB Output is partially correct
4 Partially correct 39 ms 8036 KB Output is partially correct
5 Correct 33 ms 8036 KB Output is correct
6 Correct 43 ms 8036 KB Output is correct
7 Partially correct 89 ms 8036 KB Output is partially correct
8 Correct 26 ms 8036 KB Output is correct
9 Correct 43 ms 8036 KB Output is correct
10 Partially correct 36 ms 8036 KB Output is partially correct
11 Correct 43 ms 8036 KB Output is correct
12 Correct 39 ms 8036 KB Output is correct
13 Partially correct 59 ms 8036 KB Output is partially correct
14 Partially correct 49 ms 8036 KB Output is partially correct
15 Correct 49 ms 8036 KB Output is correct
16 Partially correct 43 ms 8036 KB Output is partially correct
17 Correct 26 ms 8036 KB Output is correct
18 Correct 26 ms 8036 KB Output is correct
19 Correct 69 ms 8036 KB Output is correct
20 Correct 59 ms 8036 KB Output is correct
21 Partially correct 103 ms 8036 KB Output is partially correct
22 Partially correct 73 ms 8036 KB Output is partially correct
23 Correct 56 ms 8036 KB Output is correct
24 Correct 46 ms 8036 KB Output is correct
25 Correct 46 ms 8036 KB Output is correct
26 Correct 0 ms 8036 KB Output is correct
27 Correct 0 ms 8036 KB Output is correct
28 Correct 0 ms 8036 KB Output is correct
29 Correct 3 ms 8036 KB Output is correct
30 Correct 0 ms 8036 KB Output is correct
31 Correct 0 ms 8036 KB Output is correct
32 Correct 0 ms 8036 KB Output is correct
33 Correct 0 ms 8036 KB Output is correct
34 Correct 0 ms 8036 KB Output is correct
35 Correct 0 ms 8036 KB Output is correct
36 Correct 0 ms 8036 KB Output is correct
37 Correct 3 ms 8036 KB Output is correct
38 Correct 6 ms 8036 KB Output is correct
39 Correct 3 ms 8036 KB Output is correct
40 Correct 0 ms 8036 KB Output is correct