hdu 3329 The Flood (Flood Fill + MFSet)
用pfs,将淹没时间调整回来,然后用并查集,时间倒序插入点。
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue> using namespace std; const int N = ;
const int dx[] = { -, , , };
const int dy[] = { , -, , };
bool vis[N][N];
int mat[N][N]; typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
priority_queue<PIII> pq;
int n, m;
inline bool inmat(int x, int y) { return <= x && x < n && <= y && y < m;} void adjust() {
int ht, cx, cy, nx, ny;
while (!pq.empty()) {
PIII tmp = pq.top();
pq.pop();
ht = -tmp.first, cx = tmp.second.first, cy = tmp.second.second;
//cout << ht << ' ' << cx << ' ' << cy << endl;
for (int i = ; i < ; i++) {
nx = cx + dx[i], ny = cy + dy[i];
if (!inmat(nx, ny)) continue;
if (vis[nx][ny]) continue;
mat[nx][ny] = max(ht, mat[nx][ny]);
pq.push(PIII(-mat[nx][ny], PII(nx, ny)));
vis[nx][ny] = true;
}
}
//for (int i = 0; i < n; i++) {
//for (int j = 0; j < m; j++) cout << mat[i][j] << ' ';
//cout << endl;
//}
} struct MFS {
int fa[N * N];
void init() { for (int i = ; i < N * N; i++) fa[i] = i;}
int find(int x) { return fa[x] = fa[x] == x ? x : find(fa[x]);}
bool merge(int x, int y) {
int fx = find(x), fy = find(y);
if (fx == fy) return false;
fa[fx] = fy;
return true;
}
} mfs; int work() {
int ret = -;
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
pq.push(PIII(mat[i][j], PII(i, j)));
mat[i][j] = -;
}
}
PIII tmp;
int nid = , mg = , nx, ny;
bool sp = false;
mfs.init();
while (!pq.empty()) {
int ct = pq.top().first;
while (!pq.empty() && ct == pq.top().first) {
tmp = pq.top();
int h = tmp.first, x = tmp.second.first, y = tmp.second.second;
pq.pop();
mat[x][y] = nid++;
for (int i = ; i < ; i++) {
nx = x + dx[i], ny = y + dy[i];
if (!inmat(nx, ny)) continue;
if (mat[nx][ny] == -) continue;
mg += mfs.merge(mat[x][y], mat[nx][ny]);
}
}
if (sp) {
if (mg == nid - ) ret = ct, sp = false;
} else {
if (mg != nid - ) sp = true;
}
}
return ret;
} int main() {
int cas = ;
while (~scanf("%d%d", &n, &m) && (n || m)) {
while (!pq.empty()) pq.pop();
memset(vis, , sizeof(vis));
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
scanf("%d", &mat[i][j]);
if (i == || i == n - || j == || j == m - ) {
pq.push(PIII(-mat[i][j], PII(i, j)));
vis[i][j] = true;
}
}
}
//cout << "??" << endl;
adjust();
int ans = work();
if (ans == -) printf("Case %d: Island never splits.\n", cas++);
else printf("Case %d: Island splits when ocean rises %d feet.\n", cas++, ans);
}
return ;
}
——written by Lyon
hdu 3329 The Flood (Flood Fill + MFSet)的更多相关文章
- python自动化开发-[第七天]-面向对象
今日概要: 1.继承 2.封装 3.多态与多态性 4.反射 5.绑定方法和非绑定方法 一.新式类和经典类的区别 大前提: 1.只有在python2中才分新式类和经典类,python3中统一都是新式类 ...
- UVa 815 洪水!
https://vjudge.net/problem/UVA-815 题意:一个n*m的方格区域,共有n*m个方格,每个方格是边长为10米的正方形,整个区域的外围是无限高的高墙,给出这n*m个方格的初 ...
- UVa 725 Division (枚举)
题意 : 输入正整数n,按从小到大的顺序输出所有形如abcde/fghij = n的表达式,其中a-j恰好为数字0-9的一个排列(可以有前导0),2≤n≤79. 分析 : 最暴力的方法莫过于采用数组存 ...
- HDU 6113 度度熊的01世界【DFS/Flood Fill】
度度熊的01世界 Accepts: 967 Submissions: 3064 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- zoj 3859 DoIt is Being Flooded (MFSet && Flood Fill)
ZOJ :: Problems :: Show Problem 这题开始的时候想不到怎么调整每个grid的实际淹没时间,于是只好找了下watashi的题解,发现这个操作还是挺简单的. ZOJ3354 ...
- 图像处理之泛洪填充算法(Flood Fill Algorithm)
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- [LeetCode] Flood Fill 洪水填充
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- [Swift]LeetCode733. 图像渲染 | Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
随机推荐
- 2019.8.10 NOIP模拟测试16 反思总结【基本更新完毕忽视咕咕咕】
一如既往先放代码,我还没开始改… 改完T1滚过来了,先把T1T2的题解写了[颓博客啊] 今天下午就要走了,没想到还有送行的饯别礼,真是欣喜万分[并没有] 早上刚码完前面的总结,带着不怎么有希望的心情开 ...
- 【模板】tarjanLCA [2017年6月计划 学习tarjanLCA]
P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...
- Leetcode34.Find First and Last Position of Element in Sorted Array在排序数组中查找元素的位置
给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值,返回 [ ...
- WordPress不同分类使用不同的文章模板
倡萌昨天分享的 Custom Post Template 和 Single Post Template 可以让你自定义每篇文章的文章模板,今天来说说WordPress不同分类使用不同的文章模板. 方法 ...
- 模拟7题解 T1方程的解
方程的解 [扩展欧几里德] 首先进行特判,两个小时基本想到了,除了a!=0,b==0,a*c<0这种情况 其次就是一般情况: 首先exgcd求出ax+by=GCD(a,b)的一组任意解 然后两边 ...
- oracle 控制结构
1.if 逻辑结构 if/then 结构是最简单的条件测试,如果条件为真,则执行程序的一行或者多行,如果条件为假,则什么都不执行, 示例: if 1>2 then null; end if ; ...
- 【笔记】http1.1支持的7种请求方法
本文是本人复习http协议整理笔记,以备后续查阅. http1.1支持的7种请求方法:get.post.head.options.put.delete.trace 在internet应用中,最常用的请 ...
- HDU 1536 求解SG函数
#include<stdio.h> #include<string.h> #include<algorithm> #include<set> using ...
- hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- laravel-admin 安装(总结)
https://www.jianshu.com/p/844b05e4c45a laravel-admin 是一个可以快速帮你构建后台管理的工具,它提供的页面组件和表单元素等功能,能帮助你使用很少的代码 ...