• 坐标离散化


    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<cstring>
    #include<queue>
    using namespace std;
    int w,h,n;
    const int maxn = 500;
    int x1[maxn],x2[maxn],y1[maxn],y2[maxn];
    bool fld[maxn*6][maxn*6];
    int dx[]={0,0,1,-1};
    int dy[]={1,-1,0,0}; 
    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 tx1=x1[i]+d,tx2=x2[i]+d;
                if(1<=tx1 && tx1<=w) xs.push_back(tx1);
                if(1<=tx2 && tx2<=w) xs.push_back(tx2);    
            }    
        }
        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()
    {
        w = compress(x1,x2,w);
        h = compress(y1,y2,h);
        memset(fld,0,sizeof(fld));
        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;
                }
            }
        }
        int ans=0;
        for(int y=0;y<h;y++)
        {
            for(int x=0;x<w;x++)
            {
                if(fld[y][x]) continue;
                ans++;
                
                queue<pair<int,int> >que;
                que.push(make_pair(x,y));
                while(!que.empty())
                {
                    int sx = que.front().first;
                    int sy = que.front().second;
                    que.pop();
                    
                    for(int i=0;i<4;i++)
                    {
                        int tx=sx+dx[i],ty=sy+dy[i];
                        if(tx<0 || w<=tx || ty<0 || h<=ty) continue;
                        if(fld[ty][tx]) continue;
                        que.push(make_pair(tx,ty));
                        fld[ty][tx]=true;    
                    }    
                } 
            }
        }
        cout  << ans;
    }
    int main(void)
    {
        cin >> w >> h >> n;
        for(int i=0;i<n;i++) cin >> x1[i];
        for(int i=0;i<n;i++) cin >> x2[i];
        for(int i=0;i<n;i++) cin >> y1[i];
        for(int i=0;i<n;i++) cin >> y2[i];
        solve(); 
        return 0;
    }

  • 相关阅读:
    1组Alpha冲刺总结
    1组Beta冲刺4/5
    1组Beta冲刺5/5
    1组Alpha冲刺4/6
    1组Alpha冲刺总结
    1组Beta冲刺2/5
    1组Beta冲刺3/5
    1组Beta冲刺2/5
    1组Alpha冲刺4/6
    1组Alpha冲刺5/6
  • 原文地址:https://www.cnblogs.com/zuimeiyujianni/p/10034366.html
Copyright © 2020-2023  润新知