【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 ...
随机推荐
- Bloom Filter 原理与应用
介绍 Bloom Filter是一种简单的节省空间的随机化的数据结构,支持用户查询的集合.一般我们使用STL的std::set, stdext::hash_set,std::set是用红黑树实现的,s ...
- LayoutInflater和inflate()方法的使用方法
public static LayoutInflaterfrom(Context context) { LayoutInflaterLayoutInflater = (LayoutInflater)c ...
- PHP开发安全之近墨者浅谈(转)
==过滤输入/输出转义 过滤是Web应用安全的基础.它是你验证数据合法性的过程.通过在输入时确认对所有的数据进行过滤,你可以避免被污染(未过滤)数据在你的程序中被误信及误用.大多数流行的PHP应用的漏 ...
- Core Foundation框架介绍
Core Foundation框架介绍 **参考网址: ARC下OC对象和CF对象之间的桥接 Core Foundation框架介绍 Core Foundation框架 Core Foundation ...
- 第二篇:Power BI数据可视化之基于Web数据的报表制作(经典级示例)
前言 报表制作流程的第一步显然是从各个数据源导入数据,Power BI能从很多种数据源导入数据:如Excel,CSV,XML,以及各类数据库(SQL Server,Oracle,My SQL等),两大 ...
- [Android]如何创建一个View的分割线
如何创建一个View的分割线,如上图 我们见介绍三种可以创建看起来很不错的view的分割线,如在button之间添加分割线. 这个例子是将为LinearLayout内的三个Button间添加分割线. ...
- 进阶篇,第二章:MC与Forge的Event系统
<基于1.8 Forge的Minecraft mod制作经验分享> 这一章其实才应该是第一章,矿物生成里面用到了Event的一些内容.如果你对之前矿物生成那一章的将算法插入ORE_GEN_ ...
- centos7 部署ssserver
centos7 部署shadowsocks服务端 为什么要选centos7? 以后centos7 肯定是主流,在不重要的环境还是尽量使用新系统吧 centos7 的坑 默认可能会有firewall 或 ...
- C#中的转义字符
一些常用的转义字符: \n 换行 \b backspace,删除光标前面的一个字符 \t tab键 由多个空格组成的一个字符,具有行与行之间的对齐功能 \\ \ 如果在字符串前面加@的话: 1 ...
- fastjson反序列化
package cn.jsonlu.passguard.utils; import com.alibaba.fastjson.JSON; import java.lang.reflect.Type; ...