zoukankan      html  css  js  c++  java
  • 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;
    }


  • 相关阅读:
    Oracle DB 使用单行函数定制输出
    NDK编译多个cpp
    使用NDK编译的时候出现 undefined reference to
    linux SSSocket 简单封装
    OCP-1Z0-051-V9.02-70题
    OCP-1Z0-051-V9.02-69题
    OCP-1Z0-051-V9.02-68题
    OCP-1Z0-051-V9.02-67题
    OCP-1Z0-051-V9.02-66题
    OCP-1Z0-051-V9.02-65题
  • 原文地址:https://www.cnblogs.com/will7101/p/6506673.html
Copyright © 2011-2022 走看看