vijos P1051 送给圣诞夜的极光

调了好久。。。

#include <cstdio>
#include <queue>
using namespace std;

char a[110*110];
/**************************************
000000000000
000000000000
00----####00
00###----#00	//like dis
00##--##--00
000000000000
000000000000
**************************************/
int visited[110*110];
int cnt;
int of1x[] = {-1, 0, 1, 0};
int of1y[] = {0, -1, 0, 1};
int of2x[] = {0, 1, 2, 1, 0, -1, -2, -1};
int of2y[] = {2, 1, 0, -1, -2, -1, 0, 1};

int main(){
	int m, n;
	scanf("%d%d", &m, &n);
	getchar();
	 
	
	for(int i = 2; i < m+2; ++i){
		for(int j = 2; j < n+2; ++j){
			a[i*(n+4)+j] = getchar();
		}
		getchar();
	}
	
	n = n+4;
	queue<int> Q;
	for(int i = 0; i < (m+4)*n-1; ++i){
		if(!visited[i] && a[i] == '#'){
			int now = i;
			visited[now] = 2;
			cnt++;
			Q.push(now);
			
			do{
				//搜索附近 
				now = Q.front();
				Q.pop();
				
				int x = now%n;
				int y = now/n;
			
				if(visited[now] == 2){	//now是第二圈 
					for(int ii = 0; ii < 4; ++ii){
						int pos = (y+of1y[ii])*n + (x+of1x[ii]);
						if(!visited[pos] && a[pos] == '#'){
							visited[pos] = 1;
							Q.push(pos);
						}
					}
				}
				
				for(int ii = 0; ii < 8; ++ii){
					int pos = (y+of2y[ii])*n + (x+of2x[ii]);
					if(!visited[pos] && a[pos] == '#'){
						visited[pos] = 2;
						Q.push(pos);
					}
				}
			}while(!Q.empty());
		}
	}
	
	printf("%d", cnt);
	return 0;
}


posted @ 2015-11-14 19:03  will7101  阅读(195)  评论(0编辑  收藏  举报