Bubble Shooter

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

——————————————————————————————————————————————————————————————
其实就是泡泡龙的游戏,给你起始的地图,以及刚打出去的泡泡的位置,如果与刚打出的泡泡相连的泡泡数大于等于3,则相连的相同颜色的泡泡会掉下来,之后,没有与顶层泡泡直接或间接相连的泡泡也会掉下来。问掉下来的泡泡总数。



分析:其实就是模拟一下就可以了。首先将与起始点直接或间接相连的相同颜色的泡泡标记一下,看总数ans是否大于等于3.

奇数层偶数层分开讨论

之后要分俩种情况讨论了:



1) ans<3 。那么要将之前的标记清除,找出与顶层泡泡直接相连或间接相连的泡泡总数lt,sum-lt就是答案了。这里解决了一个特殊情况,本来以为ans<3的话,直接输出0就可以了,但其实很有可能,即使ans<3,但起始的地图也会有一些泡泡会掉下来。



2)ans>=3,这种情况下,就还是找出与顶层直接相连或间接相连的泡泡总数lt,直接sum-lt。这里就将俩种情况统一起来了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std; int dir1[6][2]= {{-1,0},{-1,-1},{1,0},{1,-1},{0,-1},{0,1}}; //偶
int dir2[6][2]= {{-1,0},{-1,1},{1,0},{1,1},{0,-1},{0,1}}; //奇
char mp[105][105];
int m,n,ans,lt;
char s; bool cheak(int x,int y)
{
if(x<0||x>=m||y<0||y>=n||mp[x][y]=='E')
return 0;
return 1;
} void dfs(int x,int y)
{
int xx,yy;
ans++;
mp[x][y]='E'; if(x%2==0)
{
for(int i=0; i<6; i++)
{
xx=x+dir1[i][0];
yy=y+dir1[i][1];
if(cheak(xx,yy)&&mp[xx][yy]==s)
dfs(xx,yy);
}
}
else
{
for(int i=0; i<6; i++)
{
xx=x+dir2[i][0];
yy=y+dir2[i][1];
if(cheak(xx,yy)&&mp[xx][yy]==s)
dfs(xx,yy);
}
} }
void dfs2(int x,int y)
{
int xx,yy;
lt++;
mp[x][y]='E';
if(x%2==0)
{
for(int i=0; i<6; i++)
{
xx=x+dir1[i][0];
yy=y+dir1[i][1];
if(cheak(xx,yy))
dfs2(xx,yy);
}
}
else
{
for(int i=0; i<6; i++)
{
xx=x+dir2[i][0];
yy=y+dir2[i][1];
if(cheak(xx,yy))
dfs2(xx,yy);
}
}
} int main()
{
int h,w;
while(~scanf("%d%d%d%d",&m,&n,&w,&h))
{
int sum=0;
for(int i=0; i<m; i++)
{
scanf("%s",mp[i]);
for(int j=0;j<strlen(mp[i]);j++)
{
if(mp[i][j]!='E')
sum++;
}
}
for(int i=1; i<m; i+=2)
{
mp[i][n-1]='E';
}
ans=0;
s=mp[w-1][h-1];
dfs(w-1,h-1);
if(ans<3)
printf("0\n");
else
{
lt=0;
for(int i=0; i<n; i++)
{
if(mp[0][i]!='E')
dfs2(0,i);
}
printf("%d\n",sum-lt);
}
}
return 0;
}

Hdu1547 Bubble Shooter 2017-01-20 18:38 44人阅读 评论(0) 收藏的更多相关文章

  1. Pots 分类: 搜索 POJ 2015-08-09 18:38 3人阅读 评论(0) 收藏

    Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11885 Accepted: 5025 Special Judge D ...

  2. c++map的用法 分类: POJ 2015-06-19 18:36 11人阅读 评论(0) 收藏

    c++map的用法 分类: 资料 2012-11-14 21:26 10573人阅读 评论(0) 收藏 举报 最全的c++map的用法 此文是复制来的0.0 1. map最基本的构造函数: map&l ...

  3. 团体程序设计天梯赛L2-003 月饼 2017-03-22 18:17 42人阅读 评论(0) 收藏

    L2-003. 月饼 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不 ...

  4. 移植QT到ZedBoard(制作运行库镜像) 交叉编译 分类: ubuntu shell ZedBoard OpenCV 2014-11-08 18:49 219人阅读 评论(0) 收藏

    制作运行库 由于ubuntu的Qt运行库在/usr/local/Trolltech/Qt-4.7.3/下,由makefile可以看到引用运行库是 INCPATH = -I/usr//mkspecs/d ...

  5. highgui.h备查 分类: C/C++ OpenCV 2014-11-08 18:11 292人阅读 评论(0) 收藏

    /*M/////////////////////////////////////////////////////////////////////////////////////// // // IMP ...

  6. UI基础:UILabel.UIFont 分类: iOS学习-UI 2015-07-01 19:38 107人阅读 评论(0) 收藏

    UILabel:标签 继承自UIView ,在UIView基础上扩充了显示文本的功能.(文本框) UILabel的使用步骤 1.创建控件 UILabel *aLabel=[[UILabel alloc ...

  7. HDU6205 Coprime Sequence 2017-05-07 18:56 36人阅读 评论(0) 收藏

    Coprime Sequence                                                        Time Limit: 2000/1000 MS (Ja ...

  8. HDU6023 Automatic Judge 2017-05-07 18:30 73人阅读 评论(0) 收藏

    Automatic Judge Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  9. hadoop容灾能力测试 分类: A1_HADOOP 2015-03-02 09:38 291人阅读 评论(0) 收藏

    实验简单来讲就是 1. put 一个600M文件,分散3个replica x 9个block 共18个blocks到4个datanode 2. 我关掉了两个datanode,使得大部分的block只在 ...

随机推荐

  1. jvm jconsole

    JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.port=60001 -Djava.rmi.server.hostname=192. ...

  2. 【解决Jira】Chrome提示Java插件因过期而遭到阻止(JIRA上传截屏截图)

    最近经常被这个问题所困扰:用Chrome访问JIRA上传截屏截图时,地址栏下面弹出通知,提示JAVA插件已过期.但是由于公司要求统一开发环境和设置,不能更新到最新版,就像这样: 结果网页上的Java就 ...

  3. PHP 设计模式 原型模式(Prototype)之深/浅拷贝

      看PHP 设计模式 原型模式(Prototype)时,衍生出一个扩展问题之 原型拷贝的浅拷贝和深拷贝问题(不管写Java还是写PHP还是写JS时都多多少少遇到过对象拷贝问题)   比如写前端页面时 ...

  4. Makefile编写 一 *****

    编译:把高级语言书写的代码转换为机器可识别的机器指令.编译高级语言后生成的指令虽然可被机器识别,但是还不能被执行.编译时,编译器检查高级语言的语法.函数与变量的声明是否正确.只有所有的语法正确.相关变 ...

  5. (转)为C# Windows服务添加安装程序

    本文转载自:http://kamiff.iteye.com/blog/507129 最近一直在搞Windows服务,也有了不少经验,感觉权限方面确定比一般程序要受限很多,但方便性也很多.像后台运行不阻 ...

  6. js事件监听机制(事件捕获)

    在前端开发过程中我们经常会遇到给页面元素添加事件的问题,添加事件的js方法也很多,有直接加到页面结构上的,有使用一些js事件监听的方法,由于各个浏览器对事件冒泡事件监听的机制不同,le浏览器只有事件冒 ...

  7. 关于&&和||

    从alert(1&&2)输出为2谈起 一.先来说说||(逻辑或),从字面上来说,只有前后都是false的时候才返回false,否则返回true. alert(true||false); ...

  8. zookeeper 节点讲解以及实际项目运用

    转自:https://www.jianshu.com/p/86acf1df6cdd 前言:最近工作不是很忙,本应该乘着闲暇的时间看书的,之前每天晚上都要翻翻的,可是自己竟然迷恋上了王晓磊 写的 卑鄙的 ...

  9. css常用属性总结:关于word-spacing和letter-spacing的使用

    前端时间项目版本迭代,修改代码时发现使用了关于word-spacing和letter-spacing.先说下使用场景,以前的项目中,经常遇到某些字符间有一些间距,我看了一些同事的代码是这么实现的: & ...

  10. Taints和Tolerations

    Taints和Tolerations和搭配使用的,Taints定义在Node节点上,声明污点及标准行为,Tolerations定义在Pod,声明可接受得污点. 可以在命令行为Node节点添加Taint ...