hdu 5925(离散化+BFS)

题意:给你一个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":" ");
        }
    }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值