hdu1547 火拼泡泡龙 bfs

来源:互联网 发布:oracle数据库命令大全 编辑:程序博客网 时间:2024/04/28 12:37

Bubble Shooter

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 176 Accepted Submission(s): 69 
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
2221
aa
a
3333
aaa
ba
bba
3331
aaa
ba
bba
3333
aaa
Ea
aab

Sample Output

3
8
3
0

简单解释下题目的意思, 火拼泡泡龙大家都玩过吧? 没玩过的赶紧去下个玩玩- –

这题就是问你, 打一个球, 然后能打掉多少个, 这个最后打的球的位置在输入的时候会告诉你.

还有就是奇数行最多可以有W个球, 偶数行最多有W-1个球.

再解释下输入.

第一行, 包括4个数字, H, W, h, w分别表示行数, 列数, 以及最后一个球的行数和列数.

这个题, 我是这样做得, 在最后一个球的位置上展开bfs, 寻找同颜色的球有多少个, 如果小于3个的话, 就直接输出0, 一个也掉不下来.

如果大于3个, 把这些球所在位置标记为 ‘E’ 也就是这个位置没有球(消掉了嘛),

然后在第一行有球的位置展开bfs, 统计能挂住多少个球, 总球数减去挂住的球数 就是消掉的和掉下去的球数.

还有一个是关于奇偶行的问题, 每个球能搜索到的旁边的球为左上, 右上, 左, 右, 左下,右下, 但奇偶行的左上,右上, 左下,右下的坐标计算又不一样. 因为球是交叉挂着的.

看图吧, 就拿输入例子3说明.

ball

(i,j)表示第i行, 第j个球

(2,1)的球在偶数行, 他的上方两球坐标为(2-1, 1), (2-1,1+1)

(3,2)的球在奇数行, 他的上方两球坐标为(3-1, 2-1), ) , (3-1, 2)

好了, 不多说, 上代码

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue> using namespace std;char mp[110][110];bool vis[110][110];int H,W,h,w;struct pos{    int x,y;};bool ok(int x, int y){    if(x<0 || x>=H)return false;    if(y<0 || y>=W)return false;    return true;}int bfs(int sx,int sy, int flag){    int mveOdd[][2]={1,-1, 1,0, -1,-1, -1,0, 0,-1, 0,1};    int mveEven[][2]={1,0, 1,1, -1,0, -1,1, 0,-1, 0,1};    int cnt=1;    //flag==0时搜索的是最后打的球    //flag==1搜索的是顶行能挂住的球    queue<pos> qu;    char clor=mp[sx][sy];    pos cur,next;    cur.x =sx,cur.y=sy;    if(0==flag)        mp[sx][sy]='E';    vis[sx][sy]=true;    qu.push(cur);    while(!qu.empty()){        cur = qu.front();qu.pop();        for(int i=0;i<6;i++){            next=cur;            if(cur.x%2==1)                next.x+=mveEven[i][0],next.y+=mveEven[i][1];            else                next.x+=mveOdd[i][0],next.y+=mveOdd[i][1];            if(!ok(next.x, next.y))continue;            if(0==flag){                if(mp[next.x][next.y]!=clor)continue;                mp[next.x][next.y]='E';            }else{                if(vis[next.x][next.y])continue;                char ch=mp[next.x][next.y];                if(!(ch>='a' && ch<='z'))continue;                vis[next.x][next.y] = true;            }            qu.push(next);            cnt++;        }    }    return cnt;}int main(){    int i,j,len,sum,ans;    //freopen("hdu1547.in", "r", stdin);    while(cin>>H>>W>>h>>w){        sum=0;        for(i=0;i<H;i++){            scanf("%s",mp[i]);            len=strlen(mp[i]);            for(j=0;j<len;j++)                if(mp[i][j]!='E')                    sum++;        }        int tmp = bfs(h-1, w-1,0);//直接掉下来的        memset(vis, false, sizeof(vis));        if(tmp<=2){            cout<<0<<endl;            continue;        }        len=strlen(mp[0]);        tmp=0;        for(i=0;i<len;i++)            if(mp[0][i]!='E' && !vis[0][i])                tmp+=bfs(0,i,1);        //cout<<tmp<<endl;        cout<<(sum-tmp)<<endl;    }    return 0;}


1 0