Coconuts
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1904 Accepted Submission(s): 553
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 apositiveinteger 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 1 3 3 1 2 2
Sample Output
Case #1: 2 1 6 Case #2: 1 8
Source
题目大意:给你一个R*C的图,图中有n个坏水果,其余全是好水果,相邻(上下左右)的好水果可以一起吃掉,吃掉一堆好水果花费一秒,问你吃完所有的好水果花费的总时间,并从小到大输出每一秒所吃的水果数。
解题思路:首先能想到搜索来解决,但是图太大,直接暴搜肯定不行,那么再读题会发现坏水果只有200个,因为我们只需要求出被坏水果包围的好水果,所以我们对图进行离散化压缩,这样最后的图形就变成了一个非常小的图,再进行暴搜就可以了,因为题目保证坏水果不会从上一直连续到下,也不会从左一直到右,所以在外面的水果一定能够连续到四条边界,通过这个就可以判断那些水果是外面的了,在外面的好水果数就是R*C - K - 被包围的。最后注意开 long long
AC代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<cstdlib>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<deque>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define finf(a, n) fill(a, a+n, INF);
#define in1(a) scanf("%d" ,&a);
#define FIN freopen("C://Users//Ёбнд©║//Desktop//in.txt","r",stdin);
#define in2(a, b) scanf("%d%d", &a, &b);
#define in3(a, b, c) scanf("%d%d%d", &a, &b, &c);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, a, b) G[a].push_back(b);
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<LL, pair<int, LL> > LLppar;
typedef pair<int, int> par;
typedef pair<LL, int> LLpar;
const int mod = 1e9+7;
const LL INF = 1e9+7;
const int N = 1010;
const double pi = 3.1415926;
LL n, m, k;
int mp[510][510];
struct node
{
int x; //原坐标
int y;
int xx; //离散坐标
int yy;
}e[510];
bool cmp1(node a, node b)
{
return a.x < b.x;
}
bool cmp2(node a, node b)
{
return a.y < b.y;
}
int l, r, cnt, ans;
int flag1, flag2, flag3, flag4;
int dx[8] = {0, 1, 0, -1};
int dy[8] = {1, 0, -1, 0};
int vis[510][510], vis1[510][510];
vector<LL> v;
bool check(int x, int y)
{
if(x <= 0 || y <= 0 || x > l || y > r || mp[x][y]) return false;
if(vis[x][y]) return false;
return true;
}
void dfs(int x, int y) //搜索每一块
{
ans ++;
vis[x][y] = cnt;
for(int i = 0; i < 4; i ++) {
int ix = x + dx[i];
int iy = y + dy[i];
if(check(ix, iy)) {
dfs(ix, iy);
}
}
}
bool check2(int x, int y)
{
if(x <= 0 || y <= 0 || x > l || y > r || mp[x][y]) return false;
if(vis1[x][y]) return false;
return true;
}
void good(int x, int y) //判断是否被包围
{
if(x == 1) flag1 = 1;
if(x == l) flag2 = 1;
if(y == 1) flag3 = 1;
if(y == r) flag4 = 1;
vis1[x][y] = 1;
for(int i = 0; i < 4; i ++) {
int ix = x + dx[i];
int iy = y + dy[i];
if((vis[x][y] == vis[ix][iy]) && check2(ix, iy)) {
good(ix, iy);
}
}
}
void init()
{
e[0].x = 0, e[0].y = 0; //初始化边界
e[0].xx = 0,e[0].yy = 0;
mem0(mp);
mem0(vis);
mem0(vis1);
v.clear();
cnt = 1; //水果种类
}
int main()
{
//FIN;
int T, t = 1, x, y;
scanf("%d", &T);
while(T --) {
scanf("%lld%lld", &n, &m);
scanf("%lld", &k);
init();
for(int i = 1; i <= k; i ++) {
scanf("%d%d", &e[i].x, &e[i].y);
}
sort(e+1, e+k+1, cmp1);
for(int i = 1; i <= k; i ++) { //对列离散化
if(e[i].x - e[i-1].x > 1) {
e[i].xx = e[i-1].xx + 2;
}else if(e[i].x - e[i-1].x == 1) e[i].xx = e[i-1].xx + 1;
else e[i].xx = e[i-1].xx;
}
l = e[k].xx;
if(e[k].x < n) l ++;
sort(e+1, e+k+1, cmp2);
for(int i = 1; i <= k; i ++) { //对行离散化
if(e[i].y - e[i-1].y > 1) {
e[i].yy = e[i-1].yy + 2;
}else if(e[i].y - e[i-1].y == 1) e[i].yy = e[i-1].yy + 1;
else e[i].yy = e[i-1].yy;
}
r = e[k].yy;
if(e[k].y < m) r ++;
for(int i = 1; i <= k; i ++) {
mp[e[i].xx][e[i].yy] = 1;
}
int sum = 0;
for(int i = 1; i <= l; i ++) {
for(int j = 1; j <= r; j ++) {
if(check(i, j)) {
flag1 = flag2 = flag3 = flag4 = 0;
ans = 0;
dfs(i, j);
good(i, j);
if(!(flag1 && flag2 && flag3 && flag4)) { //是被包围的
v.push_back(ans);
sum += ans;
}
cnt ++;
}
}
}
v.push_back(n*m-k-(LL)sum);
sort(v.begin(), v.end());
printf("Case #%d:\n", t ++);
printf("%d\n", v.size());
for(int i = 0; i < v.size()-1; i ++) {
printf("%lld ", v[i]);
}
printf("%lld\n", v[v.size()-1]);
}
return 0;
}