坐标离散化处理

<pre name="code" class="cpp">#include <bits/stdc++.h>

using namespace std;
#define maxn 505
typedef pair<int, int> P;

int W, H, N;
int X1[maxn], X2[maxn], Y1[maxn], Y2[maxn];
bool fld[maxn][maxn];
int d[2][4] = {{-1, 0, 1, 0}, {0, -1, 0, 1}};

int compress(int *x1, int *x2, int w)
{
    vector<int> xs;

    for(int i=0; i<N; i++)
        for(int d=-1; d<=1; d++)
        {
            int xx1 = x1[i] + d;
            int xx2 = x2[i] + d;
            if(xx1>=1 && xx1<=w) xs.push_back(xx1);
            if(xx2>=1 && xx2<=w) xs.push_back(xx2);
        }

    sort(xs.begin(), xs.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());

    for(int i=0; i<N; i++)
    {
        x1[i] = find(xs.begin(), xs.end(), x1[i]) - xs.begin();
        x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin();
    }

    return xs.size();
}

void solve()
{
    memset(fld, false, sizeof(fld));

    W = compress(X1, X2, W);
    H = compress(Y1, Y2, H);

    for(int i=0; i<N; i++)
        for(int y=Y1[i]; y<=Y2[i]; y++)
            for(int x=X1[i]; x<=X2[i]; x++)
                fld[y][x] = true;

    for(int y=0; y<H; y++)
    {
        for(int x=0; x<W; x++)
            cout<<fld[y][x]<<" ";
        cout<<endl;
    }

    int cnt = 0;
    for(int y=0; y<H; y++)
        for(int x=0; x<W; x++)
            if(!fld[y][x])
            {
                cnt++;
                queue<P> que;
                que.push(make_pair(y, x));

                while(!que.empty())
                {
                    P p = que.front();
                    que.pop();
                    for(int i=0; i<4; i++)
                    {
                        int ty = p.first + d[0][i];
                        int tx = p.second + d[1][i];

                        while(tx>=0 && tx<W && ty>=0 && ty<H && !fld[ty][tx])
                        {
                            fld[ty][tx] = true;
                            que.push(make_pair(ty, tx));
                        }
                    }
                }
            }

    cout<<cnt<<endl;
}

int main()
{
    while(~scanf("%d%d", &W, &H) && W + H)
    {
        cin>>N;
        for(int i=0; i<N; i++)
            scanf("%d", &X1[i]);
        for(int i=0; i<N; i++)
            scanf("%d", &X2[i]);
        for(int i=0; i<N; i++)
            scanf("%d", &Y1[i]);
        for(int i=0; i<N; i++)
            scanf("%d", &Y2[i]);

        solve();
    }
    return 0;
}

/*
10 10 5
1 1 4 9 10
6 10 4 9 10
4 8 1 1 6
4 8 10 5 10
*/



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值