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":" ");
}
}
}
解析: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":" ");
}
}
}
相关文章推荐
- HDU 5925 Coconuts 离散化 + BFS
- HDU 5925 Coconuts(离散化+bfs, 好题)
- HDU 5925 离散化+dfs
- HDU 5925 Coconuts 离散化
- Coconuts HDU - 5925 (离散化+dfs求联通块)
- HDU 5925 离散化
- hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)
- hdu 5925 离散化 搜索
- hdu 5925 Coconuts 离散化+dfs
- hdu 5925 Coconuts (构造,离散化*)
- [HDOJ5925]Coconuts(BFS,离散化,计数)
- HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
- HDU 5925 Coconuts(二维离散化权值统计 经典)
- HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
- hdu 5925 counts (二维离散化+dfs)
- hdu 4444 Walk(离散化+BFS)
- hdu_5925_Coconuts(离散化+dfs)
- hdu Coconuts 5925(二维离散化+dfs)
- HDU 5925 Coconuts [二维离散化]【杂类+思维】
- HDU_1072 Nightmare(BFS)