【HDOJ】1547 Bubble Shooter
两次BFS,一次扫描关联点。一次扫描可能掉落的情况(即再次扫描所有非爆炸的联通点)。余下未被扫描的点均爆炸。
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <iostream>
using namespace std; #define MAXN 105
char map[MAXN][MAXN];
bool visit[MAXN][MAXN];
int h, w, x, y;
int dir[][] = {-,, ,, -,, ,, ,, ,-, -,-, ,-}; typedef struct node_t {
int x, y;
node_t() {}
node_t(int xx, int yy) {
x = xx; y = yy;
}
} node_t; bool check(int x, int y) {
int ww = w; if ((x&) == )
ww = w-;
if (x< || x>h || y< || y>ww || visit[x][y])
return true;
return false;
} void bfs_Oth(int x, int y) {
int i, j, nx, ny;
queue<node_t> Q; visit[x][y] = true;
Q.push(node_t(x,y)); while (!Q.empty()) {
node_t nd = Q.front();
Q.pop();
j = (nd.x & )<<;
for (i=j; i<j+; ++i) {
nx = nd.x + dir[i][];
ny = nd.y + dir[i][];
if (check(nx, ny) || map[nx][ny]=='E')
continue;
Q.push(node_t(nx, ny));
visit[nx][ny] = true;
}
}
} void bfs() {
int ans = ;
int i, j, nx, ny;
char c = map[x][y];
queue<node_t> Q; memset(visit, false, sizeof(visit));
visit[x][y] = true;
Q.push(node_t(x,y)); while (!Q.empty()) {
node_t nd = Q.front();
Q.pop();
++ans;
j = (nd.x & )<<;
for (i=j; i<j+; ++i) {
nx = nd.x + dir[i][];
ny = nd.y + dir[i][];
if (check(nx, ny) || map[nx][ny]!=c)
continue;
Q.push(node_t(nx, ny));
visit[nx][ny] = true;
}
} if (ans < ) {
printf("0\n");
return ;
} // search the other bubbles
for (j=; map[][j]; ++j) {
if (!visit[][j] && map[][j]!='E') {
bfs_Oth(, j);
}
} for (i=; i<=h; ++i) {
for (j=; map[i][j]; ++j) {
if (!visit[i][j] && map[i][j]!='E')
++ans;
}
}
printf("%d\n", ans);
} int main() {
int i, j;
#ifndef ONLINE_JUDGE
FILE *fin = freopen("data.in", "r", stdin);
#endif
while (scanf("%d%d%d%d", &h,&w,&x,&y)!=EOF) {
for (i=; i<=h; ++i) {
scanf("%s", map[i]+);
}
bfs();
#ifndef ONLINE_JUDGE
fflush(stdout);
#endif
} return ;
}
【HDOJ】1547 Bubble Shooter的更多相关文章
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
- 【HDOJ】【1512】Monkey King
数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...
随机推荐
- Android URI简单介绍
就Android平台而言,URI主要分三个部分:scheme, authority and path.当中authority又分为host和port.格式例如以下: scheme://host:por ...
- 小胖学PHP总结1-----PHP的数据类型
PHP一共支持8种原始类型.包含4中标量类型,即:boolean(布尔型).integer(整形).float/double(浮点型)和string(字符串型):两种复合类型,即:array(数组)和 ...
- 【教训】rm -fr ./* 教训
昨晚犯了一个重大错误,运行了 rm -rf ./* 本来是要删除一个不重要的目录的,结果在它的父目录下运行了上面命令,结果...都没了... 幸好数据库文件没有被删掉,数据还在,网站程序被删掉了,不久 ...
- android SDK开发 -- TitleBar封装(一)
假设app的title 统一的都是这种左中右结构的 代码如下 <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...
- 【转】Java 读写Properties配置文件
[转]Java 读写Properties配置文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形 ...
- Asp.net中的页面跳转及post数据
/// <summary> /// This method prepares an Html form which holds all data /// in hidden field i ...
- Ext.ComponentQuery.query()
转载:http://blog.csdn.net/jiushuai/article/details/7938476 用来找特点的所有容器(Ext.container.Container)或是通过Ext. ...
- CRT detected that the application wrote to memory after end of heap buffer.
很多人的解释都不一样, 我碰到的问题是,开辟的内存空间小于操作的内存空间.也就是说,我free的内存越界了. 这是我开辟链表结构体内存的代码: PNODE Create() { int len; / ...
- pod install后出现的错误
[!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use T ...
- Visual Studio 2010 单元测试目录
单元测试的重要性这里我就不多说了,以前大家一直使用NUnit来进行单元测试,其实早在Visual Studio 2005里面,微软就已经集成了一个叫Test的专门测试插件,经过几年的发展,这个工具现在 ...