您的位置:首页 > 其它

hdu 1547 Bubble Shooter(DFS)

2016-08-05 17:43 288 查看


Bubble Shooter

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

Total Submission(s): 1059    Accepted Submission(s): 455


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

 
题意:泡泡龙游戏,注意一开始要用三个一样的泡泡才能消,是六个方向不是八个方向

思路:先用一次DFS去标记所有能达到的相同颜色的球,然后从天花板开始往下面搜索,把能标到的也标记成-1,最后把全部的球扫一遍,不是-1的就计数

最后输出即可

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 150
char a

,ma
[2*N];
int vis
[2*N];
int n,m,x2,y2;
int dir[6][2]= {-1,-1,-1,1,0,-2,0,2,1,-1,1,1};
int flag;
int check(int x,int y)
{
if(x<1||x>n||y<1||y>2*m) return 0;
if(ma[x][y]=='A'||ma[x][y]=='E') return 0;
return 1;
}
void dfs(int u,int v)
{
vis[u][v]=-1;
for(int i=0; i<6; i++)
{
int x=u+dir[i][0],y=v+dir[i][1];
if(!check(x,y)||vis[x][y]) continue;
dfs(x,y);
}
}
void check1(int u,int v,int d)
{
vis[u][v]=1;
if(d>=2) flag=1;
for(int i=0; i<6; i++)
{
int x=u+dir[i][0],y=v+dir[i][1];
if(!check(x,y)||vis[x][y]) continue;
if(ma[x][y]==ma[u][v])
check1(x,y,d+1);
}
}
int main()
{
while(~scanf("%d %d %d %d",&n,&m,&x2,&y2))
{
for(int i=1; i<=n; i++)
scanf("%s",a[i]+1);
for(int i=1; i<=n; i++)
{
if(i&1)
{
int cnt=1;
for(int j=1; j<=m; j++)
{
ma[i][cnt]=a[i][j];
ma[i][cnt+1]='A';
cnt+=2;
}
}
else
{
int cnt=2;
ma[i][1]='A';
for(int j=1; j<m; j++)
{
ma[i][cnt]=a[i][j];
ma[i][cnt+1]='A';
cnt+=2;
}
for(int j=1; j<=3; j++)
ma[i][cnt++]='A';
}
}
if(x2&1) y2=(y2-1)*2+1;
else y2*=2;
flag=0;
memset(vis,0,sizeof(vis));
check1(x2,y2,0);
if(!flag)
{
printf("0\n");
continue;
}
for(int i=1; i<=2*m; i++)
if(ma[1][i]>='a'&&ma[1][i]<='z'&&!vis[1][i])
dfs(1,i);
int ans=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=2*m;j++)
if(ma[i][j]>='a'&&ma[i][j]<='z'&&vis[i][j]!=-1)
ans++;
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: