题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5925
题意:一个棋盘上有黑点,白点,问有几块四联通的白点,输出联通块的大小
在每个黑点上花横竖线就可以把棋牌离散化,大小最大400 * 400 ,之后求离散后的棋牌白点四联通块即可
代码:
#include <bits/stdc++.h>
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
const int maxn = 500;
const int step[][2] = { {1,0} , {-1,0} , {0,1} , {0,-1} };
bool vis[maxn][maxn];
int xv[maxn],yv[maxn],x_size,y_size;
LL ans[maxn * maxn],Block_Cnt;
bool check(int x,int y){
return x >= 0 && y >= 0 && x < x_size && y < y_size && !vis[x][y];
}
void DFS(int x,int y){
queue<int> Qx,Qy;
Qx.push(x),Qy.push(y);
while(!Qx.empty()){
int nx = Qx.front(),ny = Qy.front();
Qx.pop(),Qy.pop();
if(vis[nx][ny]) continue;
vis[nx][ny] = 1;
ans[Block_Cnt] += (LL)xv[nx] * yv[ny];
for(int i = 0;i < 4;++i)
if(check(nx + step[i][0],ny + step[i][1])) Qx.push(nx + step[i][0]),Qy.push(ny + step[i][1] );
}
}
int x[maxn],y[maxn],tmpx[maxn],tmpy[maxn];
map<int,int> XHASH,YHASH;
int main(){
// freopen("rand.txt","r",stdin);
int T,ca = 0;sf("%d",&T);
while( T-- ){
int R,C;sf("%d %d",&R,&C);
int n;sf("%d",&n);
memset(vis,0,sizeof vis);
memset(ans,0,sizeof ans);
XHASH.clear(),YHASH.clear();
for(int i = 0;i < n;++i){
sf("%d %d",&x[i],&y[i]);
tmpx[i] = x[i],tmpy[i] = y[i];
}
sort(x,x + n),sort(y,y + n);
x_size = unique(x,x + n) - x;
y_size = unique(y,y + n) - y;
int pre = 0,tmp = 0;
for(int i = 0;i < x_size;++i){
if(pre + 1!= x[i]){
xv[tmp++] = x[i] - pre - 1;
xv[tmp] = 1;XHASH[x[i]] = tmp++;pre = x[i];
}
else{
xv[tmp] = 1;XHASH[x[i]] = tmp++;pre = x[i];
}
}
if(x[x_size - 1] != R){
xv[tmp++] = R - pre;
}
x_size = tmp;
pre = tmp = 0;
for(int i = 0;i < y_size;++i){
if(pre + 1!= y[i]){
yv[tmp++] = y[i] - pre - 1;
yv[tmp] = 1;YHASH[y[i]] = tmp++;pre = y[i];
}
else{
yv[tmp] = 1;YHASH[y[i]] = tmp++;pre = y[i];
}
}
if(y[y_size - 1] != C) yv[tmp++] = C - pre;
y_size = tmp;
for(int i = 0;i < n;++i){
vis[XHASH[tmpx[i]]][YHASH[tmpy[i]]] = 1;
}
Block_Cnt = 0;
for(int i = 0;i < x_size;++i){
for(int j = 0;j < y_size;++j){
if(!vis[i][j]) {
DFS(i,j);Block_Cnt++;
}
}
}
sort(ans,ans + Block_Cnt);
pf("Case #%d:\n",++ca);
pf("%I64d\n",Block_Cnt);
for(int i = 0;i < Block_Cnt;++i)
pf("%I64d%c",ans[i]," \n"[i == Block_Cnt - 1]);
}
}