Hdu1547泡泡龙
Bubble Shooter
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1069 Accepted Submission(s): 463
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.
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).
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
思路很简单,两次BFS,一次找射下来的,一次找没有与最上面那行连着的~统计一下输出就可以啦~~
对了,注意搜索的方向只有6个,因为它的排列是这样的:
* * * * *
* * * *
* * * * *所以就只有左右上面俩下面俩啦~
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; int n,m,s,t,ans,l; char mp[105][105]; bool vis[105][105]; int f[6][2]= {{1,0},{-1,0},{1,1},{-1,1},{0,-1},{0,1}}; int ff[6][2]= {{1,0},{1,-1},{0,1},{-1,0},{-1,-1},{0,-1}}; struct Node { int x,y; }; bool jug(int x,int y) { if(x<0||x>=n) return false; if(y<0||y>=m) return false; if(mp[x][y]=='E') return false; return true; } void bfs(int x,int y) { Node butt,ss; queue<Node>q; memset(vis,0,sizeof(vis)); butt.x=x; butt.y=y; vis[x][y]=1; q.push(butt); while(!q.empty()) { ss=q.front(); q.pop(); if(ss.x%2==1) { for(int i=0; i<6; i++) { int xx=ss.x+f[i][0]; int yy=ss.y+f[i][1]; if(!vis[xx][yy]&&jug(xx,yy)) { vis[xx][yy]=1; if(mp[xx][yy]==mp[ss.x][ss.y]) { ans++; butt.x=xx; butt.y=yy; q.push(butt); } } } mp[ss.x][ss.y]='E'; } else { for(int i=0; i<6; i++) { int xx=ss.x+ff[i][0]; int yy=ss.y+ff[i][1]; if(!vis[xx][yy]&&jug(xx,yy)) { vis[xx][yy]=1; if(mp[xx][yy]==mp[ss.x][ss.y]) { ans++; butt.x=xx; butt.y=yy; q.push(butt); } } } mp[ss.x][ss.y]='E'; } } } void bbfs(int x,int y) { Node butt,ss; queue<Node>q; while(!q.empty())q.pop(); butt.x=x; butt.y=y; vis[x][y]=1; q.push(butt); while(!q.empty()) { ss=q.front(); q.pop();//printf("%d %d",ss.x,ss.y); if(ss.x%2==1) { for(int i=0; i<6; i++) { int xx=ss.x+f[i][0]; int yy=ss.y+f[i][1]; if(!vis[xx][yy]&&jug(xx,yy)) { vis[xx][yy]=1; butt.x=xx; butt.y=yy; q.push(butt); } } mp[ss.x][ss.y]='E'; } else { for(int i=0; i<6; i++) { int xx=ss.x+ff[i][0]; int yy=ss.y+ff[i][1]; if(!vis[xx][yy]&&jug(xx,yy)) { vis[xx][yy]=1; butt.x=xx; butt.y=yy; q.push(butt); } } mp[ss.x][ss.y]='E'; } } } int main() { while(~scanf("%d %d %d %d",&n,&m,&s,&t)) { ans=1; for(int i=0; i<n; i++) { getchar(); scanf("%s",&mp[i]); if(i%2!=0) { mp[i][m-1]='E'; mp[i][m]='\0'; } } bfs(s-1,t-1); if(ans<3){printf("0\n");continue;} memset(vis,0,sizeof(vis)); for(l=0; l<m; l++) { if(mp[0][l]!='E'&&!vis[0][l]) bbfs(0,l); } for(int i=0; i<n; i++) { if(i%2==0) { for(int j=0; j<m; j++) { if(mp[i][j]!='E'&&!vis[i][j]) ans++; } } else { for(int j=0; j<m-1; j++) { if(mp[i][j]!='E'&&!vis[i][j]) ans++; } } } //for(int i=0;i<n;i++)printf("%s\n",mp[i]); printf("%d\n",ans); } return 0; }