HDU 5929 Coconuts(离散化+dfs)

 

Coconuts

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

 

 

 

 

一个求有多少个联通块的题目,数据量比较大,需要对点的坐标分别离散化。

 

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;

const int Size = 1024;

int T;//测试数据
int Case = 1;
int R,C;
int Q;
vector<long long>Area;//存储每一块联通块的面积
int x[Size];
int y[Size];
int temp[Size];
int lenx[Size];//x方向每一段的长度;
int leny[Size];//y方向每一段的长度;
int Pos[Size];
int cntx,cnty;
bool book[Size][Size];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
long long dfs(int x, int y)
{
	long long sum = (long long)lenx[x]*leny[y];
	book[x][y] = true;

	for(int i=0; i<4; i++)
	{
		int _x = x+dx[i];
		int _y = y+dy[i];
		if(!book[_x][_y] && _x>=1 && _x<=cntx && _y>=1 && _y<=cnty)
			sum += dfs(_x,_y);
	}
	return sum;
}
int main()
{
	cin>>T;
	while(T--)
	{
		//Initial
		Area.clear();

		//Input
		cin>>R>>C;
		cin>>Q;
		for(int i=0; i<Q; i++)
			cin>>x[i]>>y[i];

		//离散化x轴
		int index = 0;
		temp[index++] = 0;
		temp[index++] = R;
		for(int i=0; i<Q; i++)
			temp[index++] = x[i];//将bad coconuts的x坐标以及边界存起来

		sort(temp, temp+index);//对x坐标排序
		index = unique(temp, temp+index)-temp;//对x坐标去重

		cntx = 0;//x轴离散的段数
		for(int i=1; i<index; i++)//求每一段的长度
		{
			if(temp[i] > temp[i-1]+1)
				lenx[++cntx] = temp[i]-(temp[i-1]+1);

			lenx[++cntx] = 1;
			Pos[i] = cntx;//bad coconuts所在的段数
		}

		for(int i=0; i<Q; i++)
		{
			int t = lower_bound(temp,temp+index,x[i])-temp;
			x[i] = Pos[t];//离散后更新原坐标
		}

		//离散化y轴
		index = 0;
		temp[index++] = 0;
		temp[index++] = C;
		for(int i=0; i<Q; i++)
			temp[index++] = y[i];

		sort(temp, temp+index);
		index = unique(temp, temp+index)-temp;

		cnty = 0;
		for(int i=1; i<index; i++)
		{
			if(temp[i] > temp[i-1]+1)
				leny[++cnty] = temp[i]-(temp[i-1]+1);

			leny[++cnty] = 1;
			Pos[i] = cnty;
		}

		for(int i=0; i<Q; i++)
		{
			int t = lower_bound(temp, temp+index, y[i])-temp;
			y[i] = Pos[t];
		}

		//DFS
		memset(book, false, sizeof(book));
		for(int i=0; i<Q; i++)
			book[x[i]][y[i]] = true;

		for(int i=1; i<=cntx; i++)
			for(int j=1; j<=cnty; j++)
				if(!book[i][j])
					Area.push_back(dfs(i, j));

		//output
		sort(Area.begin(), Area.end());
		int len = (int)Area.size();
		cout<<"Case #"<<(Case++)<<":\n"<<len<<"\n";
		for(int i=0; i<len-1; i++)
			cout<<Area[i]<<" ";
		cout<<Area[len-1]<<endl;

	}
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值