Problem - 3329

  用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)的更多相关文章

  1. python自动化开发-[第七天]-面向对象

    今日概要: 1.继承 2.封装 3.多态与多态性 4.反射 5.绑定方法和非绑定方法 一.新式类和经典类的区别 大前提: 1.只有在python2中才分新式类和经典类,python3中统一都是新式类 ...

  2. UVa 815 洪水!

    https://vjudge.net/problem/UVA-815 题意:一个n*m的方格区域,共有n*m个方格,每个方格是边长为10米的正方形,整个区域的外围是无限高的高墙,给出这n*m个方格的初 ...

  3. UVa 725 Division (枚举)

    题意 : 输入正整数n,按从小到大的顺序输出所有形如abcde/fghij = n的表达式,其中a-j恰好为数字0-9的一个排列(可以有前导0),2≤n≤79. 分析 : 最暴力的方法莫过于采用数组存 ...

  4. HDU 6113 度度熊的01世界【DFS/Flood Fill】

    度度熊的01世界 Accepts: 967 Submissions: 3064 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...

  5. zoj 3859 DoIt is Being Flooded (MFSet && Flood Fill)

    ZOJ :: Problems :: Show Problem 这题开始的时候想不到怎么调整每个grid的实际淹没时间,于是只好找了下watashi的题解,发现这个操作还是挺简单的. ZOJ3354 ...

  6. 图像处理之泛洪填充算法(Flood Fill Algorithm)

    泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...

  7. 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能

    泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...

  8. [LeetCode] Flood Fill 洪水填充

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  9. [Swift]LeetCode733. 图像渲染 | Flood Fill

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

随机推荐

  1. 洛谷P1966 [NOIP2013提高组Day1T2]火柴排队

    P1966 火柴排队 题目描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之间的距离定义为: ∑(ai-bi) ...

  2. tomcat标准化安装

    操作系统说明: 操作系统 版本 linux red hat release 6.4 关键软件包说明: 软件包 版本 目录 运行用户 jdk-7u79-linux-x64.gz 1.7 /usr/loc ...

  3. bzoj 3895 取石子——博弈论

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3895 看题解:https://blog.csdn.net/popoqqq/article/d ...

  4. DesktopLayer.exe专杀

    这两天发现电脑卡慢. 同事电脑发现病毒,而后装上杀软后(一直在裸奔~~~),发现自己电脑也存在. DesktopLayer.exe 会有以下几个行为: 第一,会在C:\Program Files (x ...

  5. LINUX下C++编程如何获得某进程的ID

    #include <stdio.h> #include <stdlib.h> #include <unistd.h> using namespace std; pi ...

  6. BZOJ2770: YY的Treap

    原本看标题还以为是treap的题,但是实际上是线段树. 求两点的LCA相当于求区间priority最小值的位置. 然后就可以离线先离散化然后建树做了. 记录的minpos是线段树上叶子结点的节点编号. ...

  7. JavaScript--函数表达式与函数声明的区别

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. MySQL数据库操作语句(补充1)(cmd环境运行)

    一.字符串类型 enum枚举类型 /* 也叫做枚举类型,类似于单选! 如果某个字段的值只能从某几个确定的值中进行选择,一般就使用enum类型, 在定义的时候需要将该字段所有可能的选项都罗列出来: */ ...

  9. 使用php封装APP接口

    php封装APP接口 我们先来介绍Json的封装方法 json_encode函数传递中文的话,输出后是乱码的,针对这个问题我觉得有必要做一个解释: 其实json_encode对中文那不是乱码,只是js ...

  10. Directx11教程(52) 实例(instancing)的简单应用

    原文:Directx11教程(52) 实例(instancing)的简单应用 有些时候,我们需要在场景中渲染大量的重复的物体,比如体育场中的观众,森林里面的树木等等,这些物体具有相似的形状,比如很多树 ...