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): 69Bubble 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.
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).
3
简单解释下题目的意思, 火拼泡泡龙大家都玩过吧? 没玩过的赶紧去下个玩玩- –
这题就是问你, 打一个球, 然后能打掉多少个, 这个最后打的球的位置在输入的时候会告诉你.
还有就是奇数行最多可以有W个球, 偶数行最多有W-1个球.
再解释下输入.
第一行, 包括4个数字, H, W, h, w分别表示行数, 列数, 以及最后一个球的行数和列数.
这个题, 我是这样做得, 在最后一个球的位置上展开bfs, 寻找同颜色的球有多少个, 如果小于3个的话, 就直接输出0, 一个也掉不下来.
如果大于3个, 把这些球所在位置标记为 ‘E’ 也就是这个位置没有球(消掉了嘛),
然后在第一行有球的位置展开bfs, 统计能挂住多少个球, 总球数减去挂住的球数 就是消掉的和掉下去的球数.
还有一个是关于奇偶行的问题, 每个球能搜索到的旁边的球为左上, 右上, 左, 右, 左下,右下, 但奇偶行的左上,右上, 左下,右下的坐标计算又不一样. 因为球是交叉挂着的.
看图吧, 就拿输入例子3说明.
(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;}
- hdu1547 火拼泡泡龙 bfs
- hdu1547之BFS
- 火拼泡泡龙技巧以及一些技术简介
- 【搜索】泡泡龙 DFS
- 【DFS】 hdu1547 Bubble Shooter
- Hdu1547 Bubble Shooter
- 火拼俄罗斯
- 我玩"泡泡龙"的战绩
- 泡泡龙算法模拟实现C++版
- cocos2dx 写的泡泡龙2014
- 泡泡龙——代码结构规范
- Bubbles Shader in Houdini泡泡泡泡泡泡泡泡泡泡
- 电子商务网站火拼父亲节
- 慕课火拼俄罗斯方块
- 我爱泡泡龙,我爱我的人生
- 泡泡屏保
- 泡泡背景
- BZOJ4642 泡泡
- 系统架构设计——学习篇之类的设计(UML)
- Python-sorted与sort,以及模拟内置函数sorted()
- 警告 spring boot 加载beans的顺序
- HDU2056
- Topic 3
- hdu1547 火拼泡泡龙 bfs
- 命令执行顺序控制与管道
- POJ 3159Candies (差分约束)
- [网络流24题]最小路径覆盖问题
- ubuntu15.04装wine qq
- UML类图
- Eclipse的使用
- Restful Webservice与Soap Webservices区别
- tornado入门 - 静态文件处理 和 登陆装饰器