您的位置:首页 > 其它

hdu 1547 Bubble Shooter

2016-11-29 12:35 176 查看
http://acm.split.hdu.edu.cn/showproblem.php?pid=1547

Bubble Shooter

[b]Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1074    Accepted Submission(s): 467
[/b]

[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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).
 

[align=left]Output[/align]
For each test case, output an integer indicating how many bubbles will explode.
 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

3
8
3
0

 

#include<cstdio>
#include<cstdlib>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<queue>
#include<map>
#define ms(x) memset( (x),0,sizeof(x) );
using namespace std;
typedef long long int ll;
int n,m,ans;
char v[111][111];
struct node{
int x,y;
node(int tx,int ty){ x=tx,y=ty; }
};
int check(int x,int y){
if( x < 1 || x > n ) return 1;
int tt = 0;		if( x%2 == 0 ) tt = 1;	//	奇数行不减   偶数行减1
if( y < 1 || y > m -tt ) return 1;
return 0;
}
int k[2][6][2]={
{	1,0,1,1,  -1,0,-1,1,  0,-1,0,1	},	//注意是6个   不是八个
{	1,0,1,-1,  -1,0,-1,-1,  0,-1,0,1	}
};
void bfs(int x,int y){
char ch = v[x][y];
ans++,v[x][y]='E';
queue<node> q;
q.push( node(x,y) );
while( !q.empty() ){
node t = q.front();	q.pop();
for(int i = 0;i < 6;i++){
int tx = t.x + k[ t.x%2 ][i][0];
int ty = t.y + k[ t.x%2 ][i][1];
if( check(tx,ty) ) continue;
if( v[tx][ty] == ch ){
v[tx][ty] = 'E';	ans++;
q.push( node(tx,ty) );
}
}
}

}
void f(int x,int y,int &temp,int &flag){
for(int i = 0;i < 6;i++){
int tx = x + k[ x%2 ][i][0];
int ty = y + k[ x%2 ][i][1];
if( check(tx,ty) ) continue;
if( v[tx][ty] != 'E' ){
v[tx][ty] = 'E';
temp++,flag=min(flag,tx);
f(tx,ty,temp,flag);
}
}
}
void after(){
int temp,flag;
for(int i = n;i > 0;i--){
int tt = 0;		if( i%2 == 0 ) tt = 1;
for(int j = 1;j <= m-tt;j++){
if( v[i][j] != 'E' ){
v[i][j] = 'E',temp=1,flag=i;
f(i,j,temp,flag);
if( flag != 1 ) ans += temp;
}
}
}
}
//void printf(){
//	for(int i = 1;i <= n;i++) puts( v[i]+1 );
//}
int main()
{
//	freopen("E:\\workspace\\acm---C++\\acm\\1.txt","r",stdin);
int x,y;
while(scanf("%d%d%d%d",&n,&m,&x,&y)!=EOF){
ms(v);	ans=0;
for(int i = 1;i <= n;i++) scanf("%s",v[i]+1);
bfs(x,y);
if( ans < 3 ) {	puts("0");	continue;	}
//		printf();
after();
printf("%d\n",ans);

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: