Coconuts
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 747 Accepted Submission(s): 219
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
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 1 3 3 1 2 2
Sample Output
Case #1: 2 1 6 Case #2: 1 8
题意:
简单的说一下这个题,给一些点,这些点会将矩阵分成许多部分,求分成了几部分,面积从小到达排序。
出现的问题:1、普通矩阵开不了10^9; 2、普通的搜索一定超时。
解决方法:离散化
先将出现的坐标离散化,记录空白处的长和宽,Dfs搜索。
unique()的作用:将相邻的且相同的元素“删除”(也不是,是将那些元素放后边,例如:1 2 2 2 3 5 5 6 处理后是1 2 3 5 6 2 2 5),然后返回它的长度(就是处理后6的为位置)。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <queue>
#include <vector>
#include <map>
using namespace std;
const int inf = 0x3f3f3f3f;
const int Max = 500;
typedef long long int LL;
LL c[Max], r[Max], cc[Max], rr[Max];
map<int, int> rf, cf;
bool vis[Max][Max];
vector<LL> xlen, ylen;
LL sum;
void Dfs(int i, int j)
{
if(vis[i][j] || i >= xlen.size() || i < 0 || j < 0 || j >= ylen.size())
{
return ;
}
sum += xlen[i]*ylen[j];
vis[i][j] = 1;
/*四个方向Dfs*/
Dfs(i+1, j);
Dfs(i-1, j);
Dfs(i, j+1);
Dfs(i, j-1);
}
int main()
{
int K, j, i, n;
LL R, C;
int h = 0;
scanf("%d", &K);
while(K--)
{
memset(vis, 0, sizeof vis);
xlen.clear();
ylen.clear();
cf.clear();
rf.clear();
scanf("%lld %lld", &R, &C);
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
scanf("%lld %lld", &rr[i], &cc[i]);
r[i] = rr[i];
c[i] = cc[i];
}
/*对边进行处理*/
r[0] = 0;
c[0] = 0;
r[n+1] = R;
c[n+1] = C;
/*处理列*/
sort(r, r+n+2);
int numr = unique(r, r+n+2)-r;
for(i = 1; i < numr; i++)//看懂这个处理就明白这个离散化了
{
int x = r[i] - r[i-1];
if(x > 1)//**这一步的存在是**关键**,将多个空列变成一列,并储存这些空白列的长度
{
xlen.push_back(x - 1);
}
xlen.push_back(1);
rf[r[i]] = xlen.size()-1;
}
/*处理行*/
sort(c, c+n+2);
int numc = unique(c, c+n+2)-c;
for(i = 1; i < numc; i++)
{
int y = c[i] - c[i-1];
if(y > 1)
{
ylen.push_back(y-1);
}
ylen.push_back(1);
cf[c[i]] = ylen.size() - 1;
}
/*vis标记输入的点*/
for(i = 1; i <= n; i++)
{
vis[rf[rr[i]]][cf[cc[i]]] = 1;
}
/*搜索*/
vector<LL>ans;
for(i = 0; i < xlen.size(); i++)
{
for(j = 0; j < ylen.size(); j++)
{
sum = 0;
if(vis[i][j]==0)
{
Dfs(i, j);
}
if(sum)
{
ans.push_back(sum);
}
}
}
sort(ans.begin(), ans.end());
printf("Case #%d:\n", ++h);
printf("%d\n", ans.size());
for(i = 0; i < ans.size(); i++)
{
if(i)
printf(" %lld", ans[i]);
else
printf("%lld", ans[i]);
}
printf("\n");
}
return 0;
}