hdu 1547(BFS)
Bubble Shooter
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1057 Accepted Submission(s): 454

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.
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).
aa
a
3 3 3 3
aaa
ba
bba
3 3 3 1
aaa
ba
bba
3 3 3 3
aaa
Ea
aab
8
3
0
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <stdlib.h>
using namespace std;
const int N = ;
int n,m,a,b;
char graph[N][N];
bool vis[N][N];
struct Node{
int x,y;
};
bool check(int x,int y,char c){
if(x%){
if(x<||x>n||y<||y>m||vis[x][y]||graph[x][y]=='E'||graph[x][y]!=c) return false;
}else{
if(x<||x>n||y<||y>=m||vis[x][y]||graph[x][y]=='E'||graph[x][y]!=c) return false;
}
return true;
}
bool check2(int x,int y){
if(x%){
if(x<||x>n||y<||y>m||vis[x][y]||graph[x][y]=='E') return false;
}else{
if(x<||x>n||y<||y>=m||vis[x][y]||graph[x][y]=='E') return false;
}
return true;
}
int dir1[][]={{,-},{,},{,},{,},{-,},{-,}}; ///偶数行
int dir2[][]={{,-},{,},{-,-},{-,},{,-},{,}}; ///奇数行
void bfs(){
queue<Node> q;
Node s;
s.x = a,s.y = b;
vis[s.x][s.y] = true;
q.push(s);
while(!q.empty()){
Node now = q.front();
q.pop();
Node next;
if(now.x%==){
for(int i=;i<;i++){
next.x = now.x+dir1[i][];
next.y = now.y+dir1[i][];
if(!check(next.x,next.y,graph[now.x][now.y])) continue;
vis[next.x][next.y] = true;
q.push(next);
}
}else{
for(int i=;i<;i++){
next.x = now.x+dir2[i][];
next.y = now.y+dir2[i][];
if(!check(next.x,next.y,graph[now.x][now.y])) continue;
vis[next.x][next.y] = true;
q.push(next);
}
}
}
}
void bfs2(int k){
queue<Node> q;
Node s;
s.x = ,s.y = k;
vis[s.x][s.y] = true;
q.push(s);
while(!q.empty()){
Node now = q.front();
q.pop();
Node next;
if(now.x%==){
for(int i=;i<;i++){
next.x = now.x+dir1[i][];
next.y = now.y+dir1[i][];
if(!check2(next.x,next.y)) continue;
vis[next.x][next.y] = true;
q.push(next);
}
}else{
for(int i=;i<;i++){
next.x = now.x+dir2[i][];
next.y = now.y+dir2[i][];
if(!check2(next.x,next.y)) continue;
vis[next.x][next.y] = true;
q.push(next);
}
}
}
}
int main(){
while(scanf("%d%d%d%d",&n,&m,&a,&b)!=EOF){
memset(graph,,sizeof(graph));
for(int i=;i<=n;i++){
scanf("%s",graph[i]+);
}
memset(vis,false,sizeof(vis));
bfs();
int ans = ;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(vis[i][j]) ans++;
}
}
if(ans<){
printf("%d\n",);
continue;
}
for(int i=;i<=m;i++){
if(graph[][i]=='E'||vis[][i]) continue;
bfs2(i);
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(i%==&&j==m) continue;
if(graph[i][j]=='E') continue;
if(!vis[i][j]) ans++;
}
}
printf("%d\n",ans);
}
}
hdu 1547(BFS)的更多相关文章
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- HDU 1180 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...
- HDU 2531 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...
- HDU 5025 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...
- HDU 1429 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...
- HDU 1026 (BFS搜索+优先队列+记录方案)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...
- HDU 1312 (BFS搜索模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...
- HDU 1242 (BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...
随机推荐
- DOM基本代码一
dom学习基本代码第一部分 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> < ...
- ACM1829并查集
A Bug's Life Problem Description Background Professor Hopper is researching the sexual behavior of a ...
- [解决]java.io.IOException: Cannot obtain block length for LocatedBlock
在hadoop测试集群运行job的过程中发现部分运行失败,有Cannot obtain block length for LocatedBlock,使用hdfs dfs -cat ${文件}的时候也报 ...
- Enterprise Architect13 : 去掉UML图页面右侧那一道竖线
我们在使用Enterprise Architect 中,画用例图,时序图时,页面右侧有一条竖线,见下图: 如果页面元素太多,会超出竖线的范围,显得很不协调. 如果像去掉竖线,只需选择主菜单的Layou ...
- MyBatis框架的使用及源码分析(十三) ResultSetHandler
在PreparedStatementHandler中的query()方法中,用ResultSetHandler来完成结果集的映射. public <E> List<E> que ...
- mysql 数据库备份与还原,用户的创建与删除,用户的密码修改
1.备份数据库 要退出mysql rimideiMac-23:~ rimi$ mysqldump -u root -p pro >pro.sql ls 查看路径 2.恢复数据库 2.1直接 ...
- 函数式编程--响应式编程 ---android应用例子
RxJava implements this operator as create. It is good practice to check the observer’s isUnsubscribe ...
- Anniversary party(树上dp+HDU1520)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目: 题意:一个学校要办校庆,校长决定邀请员工参加,但是下属和他的直系同时参加的话,下属将会无 ...
- Super A^B mod C (快速幂+欧拉函数+欧拉定理)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759 题目:Problem Description Given A,B,C, You should quick ...
- C#利用WebClient 两种方式下载文件
WebClient client = new WebClient(); 第一种 string URLAddress = @"http://files.cnblogs.com/x4646/tr ...