HDU 5925——Coconuts离散化 + 求连通块(2016 东北赛D题)
Posted nirvana · rebirth
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 5925——Coconuts离散化 + 求连通块(2016 东北赛D题)相关的知识,希望对你有一定的参考价值。
Problem Description
TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams about eating. One day, TanBig dreams of a field of coconuts, and the field looks like a large chessboard which has R rows and C columns. In every cell of the field, there is one coconut. Unfortunately, some of the coconuts have gone bad. For sake of his health, TanBig will eat the coconuts following the rule that he can only eat good coconuts and can only eat a connected component of good coconuts one time(you can consider the bad coconuts as barriers, and the good coconuts are 4-connected, which means one coconut in cell (x, y) is connected to (x - 1, y), (x + 1, y), (x, y + 1), (x, y - 1).
Now TanBig wants to know how many times he needs to eat all the good coconuts in the field, and how many coconuts he would eat each time(the area of each 4-connected component).
Input
The first line contains a positive integer T(T≤10) which denotes the test cases. T test cases begin from the second line. In every test case, the first line contains two integers R and C, 0<R,C≤109 the second line contains an integer n, the number of bad coconuts, 0≤n≤200 from the third line, there comes n lines, each line contains two integers, xi and yi, which means in cell(xi,yi), there is a bad coconut.
It is guaranteed that in the input data, the first row and the last row will not have bad coconuts at the same time, the first column and the last column will not have bad coconuts at the same time.
Output
For each test case, output “Case #x:” in the first line, where x denotes the number of test case, one integer k in the second line, denoting the number of times TanBig needs, in the third line, k integers denoting the number of coconuts he would eat each time, you should output them in increasing order.
Sample Input
2
3 3
2
1 2
2 13 3
1
2 2
Sample Output
Case #1:
2
1 6
Case #2:
1
8
题意
-
题意:给你一个 R ∗ C R*C R∗C的矩阵和 n n n个障碍点,问你图中能分成多少个联通块?且输出每个联通块的大小。
-
数据范围: R , C < = 1 E 9 , n < = 200 R,C<=1E9 , n<=200 R,C<=1E9,n<=200
题解
- 利用障碍点进行画线(平行x、y轴),可以发现我们能得到若干个矩形,进行离散化
- 求出画线后的各个矩形的大小,然后dfs就可以了
- 分割线横竖不超过200条,矩阵也最多200*200个
AC-Code
#include <iostream>
#include <algorithm>
#include <queue>
#include <functional>
#include <string.h>
#include <map>
using namespace std;
typedef long long ll;
const int maxn = 205;
int n, m;
ll ans[maxn * maxn], sum;
int x[maxn], y[maxn];
int xx[maxn], yy[maxn];
int linex[maxn], liney[maxn];
int nx, ny;
int cntx, cnty;
map<int, int> mapx, mapy;
bool vis[maxn][maxn];
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
void init() {
nx = ny = 0;
cntx = cnty = 0;
mapx.clear(), mapy.clear();
memset(ans, 0, sizeof(ans));
memset(vis, 0, sizeof(vis));
}
bool judge(int i, int j) {
if (i < 1 || i > cntx || j < 1 || j > cnty) return false;
return !vis[i][j];
}
void dfs(int i, int j) {
if (!judge(i, j)) return;
vis[i][j] = true;
sum += 1LL * linex[i] * liney[j];
for (int k = 0; k < 4; ++k)
dfs(i + dx[k], j + dy[k]);
}
int main() {
int T;
scanf("%d", &T);
for (int z = 1; z <= T; ++z) {
init();
int n, m, nc;
scanf("%d%d%d", &n, &m, &nc);
xx[++nx] = 1, xx[+nx] = n;
yy[++ny] = 1, yy[+ny] = m;
for (int i = 1; i <= nc; ++i) {
scanf("%d%d", &x[i], &y[i]);
xx[++nx] = x[i], yy[++ny] = y[i];
}
// 离散化x轴
sort(xx + 1, xx + 1 + nx);
nx = unique(xx + 1, xx + 1 + nx) - (xx + 1);
for (int i = 1; i <= nx; ++i) {
if (xx[i] != xx[i - 1] + 1) {
linex[++cntx] = xx[i] - xx[i - 1] - 1;
}
linex[++cntx] = 1;
mapx[xx[i]] = cntx;
}
// 离散化y轴
sort(yy + 1, yy + 1 + ny);
ny = unique(yy + 1, yy + 1 + ny) - (yy + 1);
for (int i = 1; i <= ny; ++i) {
if (yy[i] != yy[i - 1] + 1) {
liney[++cnty] = yy[i] - yy[i - 1] - 1;
}
liney[++cnty] = 1;
mapy[yy[i]] = cnty;
}
// 标记缩小后的图坏点
for (int i = 1; i <= nc; ++i) {
vis[mapx[x[i]]][mapy[y[i]]] = true;
}
// 搜索连通块
int cnt = 0;
for (int i = 1; i <= cntx; ++i) {
for (int j = 1; j <= cnty; ++j) {
if (!vis[i][j]) {
sum = 0;
dfs(i, j);
ans[cnt++] = sum;
}
}
}
sort(ans, ans + cnt);
printf("Case #%d:\\n", z);
printf("%d\\n", cnt);
for (int i = 0; i < cnt; ++i) {
printf("%lld%c", ans[i], i == cnt - 1 ? '\\n' : ' ');
}
}
}
以上是关于HDU 5925——Coconuts离散化 + 求连通块(2016 东北赛D题)的主要内容,如果未能解决你的问题,请参考以下文章
HDU 5925 Coconuts 离散化+BFS (2016CCPC东北地区大学生程序设计竞赛)
[HDOJ5925]Coconuts(BFS,离散化,计数)