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. C#细说多线程(上)

    本文主要从线程的基础用法,CLR线程池当中工作者线程与I/O线程的开发,并行操作PLINQ等多个方面介绍多线程的开发.其中委托的BeginInvoke方法以及回调函数最为常用.而 I/O线程可能容易遭 ...

  2. Linux wc指令解析

    wc指令比较实用,可以统计文件中的字节数.字符数.行数.字数等. 先通过 wc --help 查看指令帮助. $ wc --help Usage: wc [OPTION]... [FILE]... o ...

  3. java scanner工具类

    import java.util.Scanner; public class ScannerTest { public static void main(String[] args) { Scanne ...

  4. 解决标准FPGA资源丰富却浪费的问题

    FPGA以计算速度快.资源丰富.可编程著称,之前一直应用于高速数字信号领域和ASIC验证.随着逻辑资源的丰富和编程工具的改进,FPGA在机器学习和硬件加速上得到越来越多的重视,目前数据中心已经大量采用 ...

  5. [转]jQuery 读取 xml

    XML 文件内容: <?xml version="1.0" encoding="UTF-8"?> <stulist> <stude ...

  6. mysql-7事务管理

    1.事务的使用场景 mysql事务主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人愿,你既需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数 ...

  7. Codeforces Round #486-F.Rain and Umbrellas题解

    一.题目链接:http://codeforces.com/contest/988/problem/F 二.题面 三.思路 很明显而且比较能想到的$dp$. 四.代码实现 #include<bit ...

  8. ElasticSearch 基础概念学习(未完)

    1.基本定义 摘自百度百科 elasticseaElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elastic ...

  9. vs2010 出现“未能将 ProteusDebugEngine 调试器附加到计算机”

    vs2010 打开项目时出现如下图错误,解决方法: 1.查看C:\Progream Files下的Internet Explorer文件夹还在不在,不在则会出现此问题: 2.可以右键项目属性-调试-勾 ...

  10. 用VB6.0实现串口通信

    Then       ' 1位或2位          'byte 类型取值范围为 0-255 ,不能为-1                   = ) & )     End IfstrHe ...