【HDOJ】3329 The Flood
超简单BFS。
/* 3329 */
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std; #define MAXN 105 typedef struct node_t {
int x, y;
node_t() {}
node_t(int xx, int yy) {
x = xx; y = yy;
}
} node_t; int n, m, h, total;
int map[MAXN][MAXN];
bool visit[MAXN][MAXN];
int dir[][] = {
-,,,,,-,,
}; inline bool check(int x, int y) {
return x< || x>=n || y< || y>=m;
} void border_bfs(int x, int y) {
int i, j, k;
int xx, yy;
queue<node_t> Q;
node_t nd; Q.push(node_t(x, y));
visit[x][y] = true; while (!Q.empty()) {
nd = Q.front();
Q.pop();
--total;
for (i=; i<; ++i) {
xx = nd.x + dir[i][];
yy = nd.y + dir[i][];
if (check(xx, yy) || visit[xx][yy] || map[xx][yy]>h)
continue;
visit[xx][yy] = true;
Q.push(node_t(xx, yy));
}
}
} void bfs(int x, int y) {
int xx, yy, s;
int i, j, k;
queue<node_t> Q;
node_t nd; Q.push(node_t(x, y));
visit[x][y] = true; while (!Q.empty()) {
nd = Q.front();
Q.pop();
--total;
for (i=; i<; ++i) {
xx = nd.x + dir[i][];
yy = nd.y + dir[i][];
if (check(xx,yy) || visit[xx][yy])
continue;
visit[xx][yy] = true;
Q.push(node_t(xx, yy));
}
}
} int main() {
int t = ;
int i, j, k, tmp;
bool flag;
int maxh; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%d %d",&n,&m)!=EOF && (n||m)) {
maxh = -;
for (i=; i<n; ++i) {
for (j=; j<m; ++j) {
scanf("%d", &map[i][j]);
if (map[i][j] > maxh)
maxh = map[i][j];
}
}
tmp = n*m;
for (h=; h<maxh; ++h) {
// handle border of map
total = tmp;
memset(visit, false, sizeof(visit));
for (j=; j<m; ++j) {
if (!visit[][j] && map[][j]<=h)
border_bfs(, j);
if (!visit[n-][j] && map[n-][j]<=h)
border_bfs(n-, j);
}
for (i=; i<n; ++i) {
if (!visit[i][] && map[i][]<=h)
border_bfs(i, );
if (!visit[i][m-] && map[i][m-]<=h)
border_bfs(i, m-);
}
// check if there exsits two or more lands
flag = false;
for (i=; i<n; ++i) {
for (j=; j<m; ++j) {
if (!visit[i][j]) {
flag = true;
break;
}
}
if (flag)
break;
}
if (flag) {
bfs(i, j);
if (total > )
break;
}
}
if (h < maxh)
printf("Case %d: Island splits when ocean rises %d feet.\n", ++t, h);
else
printf("Case %d: Island never splits.\n", ++t);
} return ;
}
【HDOJ】3329 The Flood的更多相关文章
- 【BZOJ】3329: Xorequ
[题意]给定方程x^3x=2x,求<=x和<=2^x的满足方程的正整数个数. [算法]数位DP,矩阵快速幂 [题解]异或相当于不进位加法. 移项得,x^2x=3x,又因为x+2x=3x,所 ...
- 【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 ...
随机推荐
- 详解 Spring 3.0 基于 Annotation 的依赖注入实现--转载
使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的 ...
- 在一台电脑访问另一台电脑的mysql数据库
1. 假设192.168.1.3为服务器 2. 首先在ip为192.168.1.103的机子上能够ping 通 运行->cmd >ping 192.168.1.3 检 ...
- java按照集合中元素的属性进行排序示例代码
public class Student { private String name; private int age; private int id; public Student() { sup ...
- 9.26 noip模拟试题
魔术球问题弱化版(ball.c/.cpp/.pas) 题目描述 假设有 n 根柱子,现要按下述规则在这 n 根柱子中依次放入编号为 1,2,3,…的球. (1)每次只能在某根柱子的最上面放球. (2) ...
- C#生成缩略图不清晰模糊问题的解决方案!
之前网上找了个生成缩略图的代码,改了改直接用了.问题来了,等比例缩略图时总是发现左边.上边的边线大概有一像素的白边,领导不乐意了,那咱就改吧.图片放大了才发现,那个好像是渐变的颜色,晕,这样的功能领导 ...
- PetaPoco 存储过程
1 执行不带参数的存储过程 public List<dynamic> ceshiProc() { string sql = @"EXEC [dbo].[p_ceshi1]&quo ...
- HTML5 autocomplete属性、表单自动完成
autocomplete属性 1.定义autocomplete属性规范表单是否启用自动完成功能.自动完成允许浏览器对字段的输入,是基于之前输入的值.2.应用范围autocomplete使用<fo ...
- Android TV 模拟器启动
模拟器启动错误 使用IntelCPU的模拟器 http://software.intel.com/en-us/android/articles/intel-hardware-accelerated-e ...
- 1、大部分社交平台接口不支持https协议。
参考文献来自:http://wiki.mob.com/ios9-%E5%AF%B9sharesdk%E7%9A%84%E5%BD%B1%E5%93%8D%EF%BC%88%E9%80%82%E9%85 ...
- css 不确定元素宽度的水平居中
对于一个不确定宽度的元素居中,我们想到使用的方法是 text-align:center; 或者 margin:0 auto; text-align只对行内元素有效,对于块元素我们要用margin,块元 ...