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,求到达终点的最少耗时. 解题思路: ...
随机推荐
- 【线段树】【P3740】 [HAOI2014]贴海报
传送门 Description Bytetown城市要进行市长竞选,所有的选民可以畅所欲言地对竞选市长的候选人发表言论.为了统一管理,城市委员会为选民准备了一个张贴海报的electoral墙. 张贴规 ...
- Codeforces Round #514 (Div. 2):D. Nature Reserve(二分+数学)
D. Nature Reserve 题目链接:https://codeforces.com/contest/1059/problem/D 题意: 在二维坐标平面上给出n个数的点,现在要求一个圆,能够容 ...
- HDU 4313树形DP
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDU3308 线段树(区间合并)
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- maven中net.sf.json报错的解决方法(转载)
原文:http://www.cnblogs.com/winner-0715/p/5928514.html 今天在用maven添加net.sf.json的jar包的时候,代码如下: <depend ...
- Spring Boot 打包部署
一.打包成jar并部署 1.工程--右键选择运行配置: 在Goals中输入: org.apache.maven.plugins:maven-jar-plugin:.RELEASE:repackage ...
- 线程阶段性总结——APM,ThreadPool,Task,TaskScheduler ,CancellationTokenSource
不管我们使用thread,threadPool,task,还是APM异步,本质都是在使用多线程.对于新手来说,不太敢用多线程的原因,就我个人的体验来说,就是对多线程的异常捕获方式或时机缺乏了解,而一旦 ...
- vijos 1180 选课 树形DP
描述 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<300)门的选修课程,每个学生可选课程的数量M是给定的.学生选修了这M门课并考核通过就能获得 ...
- 【poj1830-开关问题】高斯消元求解异或方程组
第一道高斯消元题目~ 题目:有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为开就变为关 ...
- bzoj 1774: [Usaco2009 Dec]Toll 过路费 ——(改)floyd
Description 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都 要向农夫 ...