程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu1547之BFS

hdu1547之BFS

編輯:C++入門知識

Bubble Shooter
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 596    Accepted Submission(s): 239


Problem Description
Bubble shooter is a popular game. You can find a lot of versions from the Internet.

 

The goal of this game is to clean the bubbles off the field. Every time you just point the cannon to where you want the next bubble to go, and if three or more of bubbles with the same color came together (including the newly shot bubble), they will detonate. After the first explode, if some bubbles are disconnected from the bubble(s) in the topmost row, they will explode too.

In this problem, you will be given an arranged situation of bubbles in the field and the newly shot bubble. Your program should output the total number of bubbles that will explode.

 

Input
There are multiple test cases. Each test case begins with four integers H (the height of the field, 2 <= H <= 100), W (the width of the field, 2 <= W <= 100, in the picture above, W is 10), h (the vertical position of the newly shot bubble, count from top to bottom, and the topmost is counted as 1) and w (the horizontal position of the newly shot bubble, count from left to right, and the leftmost is counted as 1).
Then H lines follow, the odd lines will contain W characters while the even lines will contain W-1 characters (refer to the picture above). Each character will be either a lowercase from 'a' to 'z' indicating the color of the bubble in that position, or a capital letter 'E' indicating an empty position. You may assure the arranged situation is always valid (all the bubbles are directly or indirectly connected with at least one bubble in the topmost row, and the position of newly shot bubble is never empty).
 

Output
For each test case, output an integer indicating how many bubbles will explode.
 

Sample Input
2 2 2 1
aa
a
3 3 3 3
aaa
ba
bba
3 3 3 1
aaa
ba
bba
3 3 3 3
aaa
Ea
aab

Sample Output
3
8
3
0
題意:輸入n行m列,和發出小球的到達的位置h,w,用a~z表示不同顏色的小球,E表示該位置為空,另外奇數行有m個小球位置,偶數行有m-1個小球位置

求發射出該小球後掉落的小球數量有多少,已知相同顏色的小球連通數量>=3就會掉落和懸空的小球也會掉落(即周圍沒有小球與它相連)

 


分析:先從發射出的小球那開始廣搜,判斷相同顏色連通的數量是否>=3,是則將這些位置標記為空

然後對第一行小球位置開始搜索,所有搜索到的小球都不是懸空的,即不會掉落,沒搜索到的會掉落

 

#include<iostream>   
#include<cstdio>   
#include<cstdlib>   
#include<cstring>   
#include<string>   
#include<queue>   
#include<algorithm>   
#include<map>   
#include<iomanip>   
#define INF 99999999   
using namespace std;  
  
const int MAX=100+10;  
char Map[MAX][MAX];  
int n,m,sx,sy,s[MAX*MAX];  
int dir0[6][2]={0,1,0,-1,1,0,-1,0,-1,-1,1,-1};//偶數行能走得方向(行下標從0開始)   
int dir1[6][2]={0,1,0,-1,1,0,-1,0,-1,1,1,1};//奇數行能走得方向    
  
int BFS(bool flag,int sx,int sy){  
    char ch=Map[sx][sy];  
    Map[sx][sy]='E';  
    int sum=0;  
    queue<int>q;  
    int oq,a,b;  
    q.push(sx*m+sy);//將第x*n+y個點入隊   
    while(!q.empty()){  
        oq=q.front();  
        q.pop();  
        if(flag)s[sum]=oq;//先記錄點,如果sum<3則需要還原點    
        ++sum;  
        int x=oq/m,y=oq%m;  
        for(int i=0;i<6;++i){  
            if(x%2)a=x+dir1[i][0],b=y+dir1[i][1];  
            else a=x+dir0[i][0],b=y+dir0[i][1];  
            if(a<0 || b<0 || a>=n || (a%2 && b>=m-1) || b>=m)continue;  
            if(Map[a][b] == 'E')continue;//表示該點為空   
            if(flag && Map[a][b] != ch)continue;//查找周圍連通且相同顏色的球    
            Map[a][b]='E';  
            q.push(a*m+b);  
        }  
    }  
    if(flag && sum<3){  
        for(int i=0;i<sum;++i)Map[s[i]/m][s[i]%m]=ch;  
    }  
    return sum;  
}  
  
int main(){  
    while(cin>>n>>m>>sx>>sy){  
        --sx,--sy;  
        for(int i=0;i<n;++i)cin>>Map[i];  
        int ans=0,sum=0;//sum表示總的球數   
        for(int i=0;i<n;++i){  
            for(int j=0;j<m;++j){  
                if(Map[i][j]>='a' && Map[i][j]<='z')++sum;  
            }  
        }  
        BFS(true,sx,sy);  
        for(int i=0;i<m;++i){  
            if(Map[0][i] != 'E')ans+=BFS(false,0,i);//查詢與頂上連通的球    
        }  
        cout<<sum-ans<<endl;  
    }  
    return 0;  
}  

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;

const int MAX=100+10;
char Map[MAX][MAX];
int n,m,sx,sy,s[MAX*MAX];
int dir0[6][2]={0,1,0,-1,1,0,-1,0,-1,-1,1,-1};//偶數行能走得方向(行下標從0開始)
int dir1[6][2]={0,1,0,-1,1,0,-1,0,-1,1,1,1};//奇數行能走得方向 

int BFS(bool flag,int sx,int sy){
	char ch=Map[sx][sy];
	Map[sx][sy]='E';
	int sum=0;
	queue<int>q;
	int oq,a,b;
	q.push(sx*m+sy);//將第x*n+y個點入隊
	while(!q.empty()){
		oq=q.front();
		q.pop();
		if(flag)s[sum]=oq;//先記錄點,如果sum<3則需要還原點 
		++sum;
		int x=oq/m,y=oq%m;
		for(int i=0;i<6;++i){
			if(x%2)a=x+dir1[i][0],b=y+dir1[i][1];
			else a=x+dir0[i][0],b=y+dir0[i][1];
			if(a<0 || b<0 || a>=n || (a%2 && b>=m-1) || b>=m)continue;
			if(Map[a][b] == 'E')continue;//表示該點為空
			if(flag && Map[a][b] != ch)continue;//查找周圍連通且相同顏色的球 
			Map[a][b]='E';
			q.push(a*m+b);
		}
	}
	if(flag && sum<3){
		for(int i=0;i<sum;++i)Map[s[i]/m][s[i]%m]=ch;
	}
	return sum;
}

int main(){
	while(cin>>n>>m>>sx>>sy){
		--sx,--sy;
		for(int i=0;i<n;++i)cin>>Map[i];
		int ans=0,sum=0;//sum表示總的球數
		for(int i=0;i<n;++i){
			for(int j=0;j<m;++j){
				if(Map[i][j]>='a' && Map[i][j]<='z')++sum;
			}
		}
		BFS(true,sx,sy);
		for(int i=0;i<m;++i){
			if(Map[0][i] != 'E')ans+=BFS(false,0,i);//查詢與頂上連通的球 
		}
		cout<<sum-ans<<endl;
	}
	return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved