【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 ...
随机推荐
- 适配iOS7uinavigationbar遮挡tableView的问题
//适配iOS7uinavigationbar遮挡tableView的问题 if([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0 ...
- [AngularJS] 5 simple ways to speed up your AngularJS application
Nowdays, Single page apps are becoming increasingly popular among the fornt-end developers. It is th ...
- 18、MySQL内存体系架构及参数总结
内存结构: Mysql 内存分配规则是:用多少给多少,最高到配置的值,不是立即分配 图只做大概参考 全局缓存包括: global buffer(全局内存分配总和) = innodb_buffer ...
- poj2993 poj2669
扯淡题. 2993 #include <iostream> #include <stdio.h> #include <string> #include <st ...
- RT: TCP REUSEADDR or REUSEPORT
Welcome to the wonderful world of portability... or rather the lack of it. Before we start analyzing ...
- TCP/IP协议原理与应用笔记07:HTTP、TCP/IP与socket区别
1. TCP/IP协议与HTTP协议区别: HTTP 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件 ...
- TCP/IP协议原理与应用笔记06:OSI参考模型全称
1. ISO是一个组织,OSI是一个模型. 2. OSI不是协议,是网络体系结构的概念模型 3. OSI参考模型全称为: Open System Interconnection Reference M ...
- Shell 获取当前时间
#! /bin/bash time=`date '+%Y-%m-%d %H:%M:%S'` # echo $time sql='SELECT `username`, `password` FROM ` ...
- HDU 1756 Cupid's Arrow 判断点在多边形的内部
Cupid's Arrow Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- shell脚本中echo显示内容带颜色
转自:http://www.cnblogs.com/lr-ting/archive/2013/02/28/2936792.html shell脚本中echo显示内容带颜色显示,echo显示带颜色,需要 ...