超简单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的更多相关文章

  1. 【BZOJ】3329: Xorequ

    [题意]给定方程x^3x=2x,求<=x和<=2^x的满足方程的正整数个数. [算法]数位DP,矩阵快速幂 [题解]异或相当于不进位加法. 移项得,x^2x=3x,又因为x+2x=3x,所 ...

  2. 【HDOJ】4729 An Easy Problem for Elfness

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

  3. 【HDOJ】【3506】Monkey Party

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

  4. 【HDOJ】【3516】Tree Construction

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

  5. 【HDOJ】【3480】Division

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

  6. 【HDOJ】【2829】Lawrence

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

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

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

  8. 【HDOJ】【3530】Subsequence

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

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

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

随机推荐

  1. 可以直接拿来用的15个jQuery代码片段

    jQuery里提供了许多创建交互式网站的方法,在开发Web项目时,开发人员应该好好利用jQuery代码,它们不仅能给网站带来各种动画.特效,还会提高网站的用户体验. 本文收集了15段非常实用的jQue ...

  2. C++链表与键值对

    <算法>一书中,在算法3.1中提到了Map的实现,这里根据书上的思想,用单向链表简单写了写. #ifndef SEQUENTIAL_H #define SEQUENTIAL_H templ ...

  3. SPFA 小优化*2

    /* bzoj 2763 SPFA小优化 循环队列+SLF 顺面改掉自己之前手打qeueu的坏毛病*/ #include<iostream> #include<cstring> ...

  4. ASP.NET JSON的序列化和反序列化 之 Newtonsoft.Json

    我们用到的类库为:Newtonsoft.Json,通过VS工具中NuGet程序包可以下载. 一:对象转json-序列化 public class Student { public int ID { g ...

  5. Asp.net中向前端输出JS的一些调用

    最近突然写ASP.NET项目,用到向前台输出JS脚本,但是以前在MVC里是通过异步或者一些方法来调用,但是ASP.net用到的很少.在网上找到一个HELPER.CS.保存一下,以后再用. using ...

  6. 读取xml字符串

    string strXml = @"<MessageData><pm_id>10</pm_id><pm_title>这是公司或者产品的名称&l ...

  7. The performance between the 'normal' operation and the 'shift' operation.

    First, I gonna post my test result with some code: //test the peformance of the <normal operation ...

  8. youphp学习整理

    <?php //后台公共模块 // _list 数据显示 // add 添加/编辑 视图 // insert 添加处理函数 // edit 添加/编辑 视图 // update 更新处理函数 / ...

  9. 那些常用的eclipse快捷键

    用了很久的eclipse了,有些快捷键常用,有的偶尔使用,现在记下常用的快捷键,以便大家和自己查用(持续更新) 这些快捷键都可以在[window]-[preferences]-[general]-[k ...

  10. java 对于url地址的实体符号的处理

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 <dependency> <g ...