您的位置:首页 > 其它

hdu 5925(离散化+BFS)

2017-08-17 10:56 267 查看
题意:给你一个rxc的图,里面包含n个障碍物,求区域个数和每个区域的大小

解析:r 和 c 很大(10e9), 而n只有200 ,摆明了使用离散化

离散化:http://blog.csdn.net/gokou_ruri/article/details/7723378

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long int LL;
#define maxn 500
int r,c,n,T,k;

int x[maxn];
int y[maxn];
int cntx;
int cnty;

int lenx[maxn];
int leny[maxn];

int temp[maxn];
int pos[maxn];
int index;

int map[maxn][maxn];
int way[4][2] = {1,0,-1,0,0,1,0,-1};
LL ans[maxn];
int cnt;
struct node{
int x,y;
};
LL bfs(int _x,int _y)
{
node s;
s.x = _x;
s.y = _y;
queue<node>Q;
Q.push(s);
LL res = 0;
while( !Q.empty()){
node now = Q.front();
Q.pop();
res += (LL)lenx[now.x]*leny[now.y] ;
for(int i = 0 ; i < 4 ; ++i){
int nx = now.x + way[i][0];
int ny = now.y + way[i][1];
if( 0 <= nx && cntx > nx && 0 <= ny && cnty > ny && !map[nx][ny] ){
node nxt;
nxt.x = nx;
nxt.y = ny;
map[nx][ny] = 1;
Q.push(nxt);
}
}
}
return res;
}

int main()
{
cin >> T;
while( T-- )
{
cin >> r >> c ;
cin >> n;
for(int i = 0 ; i < n ; ++i){
scanf("%d %d",&x[i],&y[i]);
}
//离散化x轴
cntx = 0;
index = 0;
temp[index++] = 0;
temp[index++] = r;
for(int i =0 ; i < n ; ++i){
temp[index++] = x[i];
}
sort(temp,temp+index);
index = unique(temp,temp+index) - temp;
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 - 1;
}
for(int i = 0 ; i < n ; ++i){
int t = lower_bound(temp,temp+index,x[i]) - temp;
x[i] = pos[t];
}
//离散化y轴
cnty = 0;
index = 0;
temp[index++] = 0;
temp[index++] = c;
for(int i =0 ; i < n ; ++i){
temp[index++] = y[i];
}
sort(temp,temp+index);
index = unique(temp,temp+index) - temp;
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 - 1;
}
for(int i = 0 ; i < n ; ++i){
int t = lower_bound(temp,temp+index,y[i]) - temp;
y[i] = pos[t];
}

memset(map,0,sizeof(map));
for(int i = 0 ; i < n ; ++i)
map[ x[i] ][ y[i] ] = 1;

cnt = 0;
for(int i = 0 ; i < cntx ; ++i){
for(int j = 0 ; j < cnty ; ++j){
if( !map[i][j] ){
map[i][j] = 1;
ans[cnt++] = bfs(i,j);
}
}
}

printf("Case #%d:\n",++k);
printf("%d\n",cnt);
sort(ans,ans+cnt);
for(int i = 0 ; i < cnt; ++i){
printf("%lld%s",ans[i],(i==cnt-1)?"\n":" ");
}
}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: