Coconuts
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 693 Accepted Submission(s): 199
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).
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.
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
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5925
这个题我真是。。。因为一个long long 数组,我交了5发wa,我真是!@#¥……&*()
题目的题意是说Mr.Frog想要在一张地图上吃椰子,他只能上下左右移动,现在地图上有一些坏椰子,可看成是障碍,问你这张地图有几个连通块,每个连通块有多少个格子。
如果我们不考虑数据范围,这就是一个很简单的dfs,但是坐标范围为10^9,所以我们离散化两个坐标轴,用代码讲吧。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
#define LL long long
using namespace std;
struct node{
LL i,j;
};
LL vis[500][500];
vector<LL>nx,ny;
LL sum;
int dx[]={0,-1,0,1};
int dy[]={1,0,-1,0};
void dfs(int x,int y){//这个dfs很简单,标记一下直接暴力即可
if(vis[x][y]||x>=(int )nx.size()||x<0||y>=(int )ny.size()||y<0)
return;
sum+=(LL)nx[x]*(LL)ny[y];//这里用相乘计算每个小矩形的格子数
vis[x][y]=1;
for(int i=0;i<4;i++){
dfs(x+dx[i],y+dy[i]);
}
}
int main(){
int t;
scanf("%d",&t);
int cnt=0;
while(t--){
cnt++;
nx.clear();
ny.clear();
LL n,m;
scanf("%lld%lld",&m,&n);
int top1=0;
int top2=0;
int xx[500],yy[500];
xx[top1++]=0;
xx[top1++]=n;
yy[top2++]=0;
yy[top2++]=m;
LL k;
vector<node> point(500);
scanf("%lld",&k);
for(int i=0;i<k;i++){
scanf("%lld%lld",&point[i].j,&point[i].i);//记录障碍物坐标
}
printf("Case #%d:\n",cnt);
for(LL i=0;i<k;i++){
xx[top1++]=point[i].i;//把纵坐标和横坐标分开,准备离散化用
yy[top2++]=point[i].j;
}
sort(xx,xx+top1);
sort(yy,yy+top2);
LL xxx=unique(xx,xx+top1)-xx;//离散化坐标
LL yyy=unique(yy,yy+top2)-yy;
map<LL,LL> x1,y1;
for(LL i=1;i<xxx;i++){
if(xx[i]-xx[i-1]>1)
nx.push_back(xx[i]-xx[i-1]-1);
nx.push_back(1);
x1[xx[i]]=(int )nx.size()-1 ;
}
for (LL j=1;j<yyy;j++){
if(yy[j]-yy[j-1]>1)
ny.push_back(yy[j]-yy[j-1]-1);
ny.push_back(1);
y1[yy[j]]=(int )ny.size()-1;
}
memset(vis,0,sizeof(vis));
for(int i=0;i<k;i++){
vis[x1[point[i].i]][y1[point[i].j]]=1;
}
long long xin[500];//这个数组是long long ,当时一激动敲成了int,白白5发罚时,100分钟啊!!
int top=0;
for(int i=0;i<(int )nx.size();i++){
for(int j=0;j<(int )ny.size();j++){
sum=0;
if (!vis[i][j])
dfs(i,j);
if(sum)
xin[top++]=sum;
}
}
sort(xin,xin+top);
cout<<top<<endl;
for (int i=0;i<top;i++){
printf(i==0?"%lld":" %lld",xin[i]);
}
cout<<endl;
}
}