两次BFS,一次扫描关联点。一次扫描可能掉落的情况(即再次扫描所有非爆炸的联通点)。余下未被扫描的点均爆炸。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #include <cstdlib>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. #define MAXN 105
  9. char map[MAXN][MAXN];
  10. bool visit[MAXN][MAXN];
  11. int h, w, x, y;
  12. int dir[][] = {-,, ,, -,, ,, ,, ,-, -,-, ,-};
  13.  
  14. typedef struct node_t {
  15. int x, y;
  16. node_t() {}
  17. node_t(int xx, int yy) {
  18. x = xx; y = yy;
  19. }
  20. } node_t;
  21.  
  22. bool check(int x, int y) {
  23. int ww = w;
  24.  
  25. if ((x&) == )
  26. ww = w-;
  27. if (x< || x>h || y< || y>ww || visit[x][y])
  28. return true;
  29. return false;
  30. }
  31.  
  32. void bfs_Oth(int x, int y) {
  33. int i, j, nx, ny;
  34. queue<node_t> Q;
  35.  
  36. visit[x][y] = true;
  37. Q.push(node_t(x,y));
  38.  
  39. while (!Q.empty()) {
  40. node_t nd = Q.front();
  41. Q.pop();
  42. j = (nd.x & )<<;
  43. for (i=j; i<j+; ++i) {
  44. nx = nd.x + dir[i][];
  45. ny = nd.y + dir[i][];
  46. if (check(nx, ny) || map[nx][ny]=='E')
  47. continue;
  48. Q.push(node_t(nx, ny));
  49. visit[nx][ny] = true;
  50. }
  51. }
  52. }
  53.  
  54. void bfs() {
  55. int ans = ;
  56. int i, j, nx, ny;
  57. char c = map[x][y];
  58. queue<node_t> Q;
  59.  
  60. memset(visit, false, sizeof(visit));
  61. visit[x][y] = true;
  62. Q.push(node_t(x,y));
  63.  
  64. while (!Q.empty()) {
  65. node_t nd = Q.front();
  66. Q.pop();
  67. ++ans;
  68. j = (nd.x & )<<;
  69. for (i=j; i<j+; ++i) {
  70. nx = nd.x + dir[i][];
  71. ny = nd.y + dir[i][];
  72. if (check(nx, ny) || map[nx][ny]!=c)
  73. continue;
  74. Q.push(node_t(nx, ny));
  75. visit[nx][ny] = true;
  76. }
  77. }
  78.  
  79. if (ans < ) {
  80. printf("0\n");
  81. return ;
  82. }
  83.  
  84. // search the other bubbles
  85. for (j=; map[][j]; ++j) {
  86. if (!visit[][j] && map[][j]!='E') {
  87. bfs_Oth(, j);
  88. }
  89. }
  90.  
  91. for (i=; i<=h; ++i) {
  92. for (j=; map[i][j]; ++j) {
  93. if (!visit[i][j] && map[i][j]!='E')
  94. ++ans;
  95. }
  96. }
  97. printf("%d\n", ans);
  98. }
  99.  
  100. int main() {
  101. int i, j;
  102. #ifndef ONLINE_JUDGE
  103. FILE *fin = freopen("data.in", "r", stdin);
  104. #endif
  105. while (scanf("%d%d%d%d", &h,&w,&x,&y)!=EOF) {
  106. for (i=; i<=h; ++i) {
  107. scanf("%s", map[i]+);
  108. }
  109. bfs();
  110. #ifndef ONLINE_JUDGE
  111. fflush(stdout);
  112. #endif
  113. }
  114.  
  115. return ;
  116. }

【HDOJ】1547 Bubble Shooter的更多相关文章

  1. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  2. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  3. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  4. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  5. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  6. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  7. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  8. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

  9. 【HDOJ】【1512】Monkey King

    数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...

随机推荐

  1. Haskell 差点儿无痛苦上手指南

    趁着自己重装Linux 虚拟机的机会,把安装 haskell 的过程记录一下,顺便帮那些还犹豫徘徊在haskell门外的读者入门. 基本概念: Haskell : 是一门通用函数式语言,差点儿能够进行 ...

  2. 合肥三洋股份,惠而浦家电携四大品牌-Take ,所有的市场

        大家都知道,数家电企业的日子并不好过.一方面,产品同质化竞争越发激烈.家电市场已进入了恶性价格战时代.还有一方面,消费者对家电产品的需求越发多元化.个性化.这意味着无法满足消费者需求的产品非常 ...

  3. Python学习之四【变量】

    变量:用于引用(绑定)对象的标识符 语法: >>变量名=对象 (数值,表达式等) 如计算圆的面积 PI=3.14 redius:12.3 area=PI*radius**2(**在pyth ...

  4. JBoss EAP6/AS7/WildFly: How to Use Properties Files Outside Your Archive--reference

    Introduction I’d like to preface this post with, really, all properties files should be inside your ...

  5. linux服务器上的php代码通过nginx发布,解决pathinfo模式问题

    附件1为修改前的正常访问php配置文件      附件2为修改后的能通过url地址访问php项目的配置文件    具体操作网址 如下:www.itokit.com/2012/0308/73275.ht ...

  6. Log4j使用教程 (转载http://www.codeceo.com/article/log4j-usage.html)

    日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方便的日志记录.在apache网站:jakarta.apache.org/log4j 可以免费下载到Log ...

  7. 《Android开发艺术探索》读书笔记 (2) 第2章 IPC机制

    2.1 Android IPC简介 (1)任何一个操作系统都需要有相应的IPC机制,Linux上可以通过命名通道.共享内存.信号量等来进行进程间通信.Android系统不仅可以使用了Binder机制来 ...

  8. 移动页面缩放方法之(二)控制HTML

    <!DOCTYPE HTML> <html lang="zh-cn"> <head> <meta http-equiv="Con ...

  9. ASP.NET 多线程 监控任务执行情况,并显示进度条

    关于多线程的基本概念和知识在本文中不多讲,而且我懂的也不是很透,说的太多误人子弟...对于我来说,做本文提到的功能够用就行,等实现其他效果不够用的时候,再深入研究 推荐看园子里的两篇博客应该就有个基本 ...

  10. HTML5 Blob与ArrayBuffer、TypeArray和字符串String之间转换

    1.将String字符串转换成Blob对象 //将字符串 转换成 Blob 对象 var blob = new Blob(["Hello World!"], { type: 'te ...