UVALive 6663 Count the Regions 离散+bfs染色_(:зゝ∠)_

本文探讨了一段使用C++解决复杂路径问题的代码实现,通过构建节点结构、使用广度优先搜索算法来判断路径是否存在,展示了路径查找在算法领域的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:点击打开链接

gg。。==

#include <cstdio>
#include <cstring>
#include<iostream>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <string>
using namespace std;
#define ll long long
#define inf 10000000
#define N 55
typedef pair<int,int> pii;
struct node{
    int lx, ly, rx, ry;
    void put(){printf("    (%d,%d) - (%d,%d) ",lx,ly,rx,ry);}
}a[N];
vector<int>X,Y;
ll mp[200][200];
int vis[200][200];
int step[4][2] = {-1,0, 0,-1, 0,1,  1,0};
int n;
void bfs(int x, int y){
    queue<int>qx, qy;
    qx.push(x); qy.push(y);
    vis[x][y]=1;
    while(!qx.empty()) {
        int ux = qx.front(); qx.pop();
        int uy = qy.front(); qy.pop();
        for(int i = 0; i < 4; i++)
        {
            int vx = step[i][0] + ux, vy = step[i][1] + uy;
            if(vx<0 || vx>=200 || vy<0 || vy>=200)continue;
            if(vis[vx][vy])continue;
            if(mp[ux][uy]!=mp[vx][vy])continue;
            vis[vx][vy] = 1;
            qx.push(vx); qy.push(vy);
        }
    }
}
void input(){
    X.clear(); Y.clear();
    for(int i = 1; i <= n; i++)
    {
        scanf("%d %d %d %d",&a[i].lx,&a[i].ly,&a[i].rx,&a[i].ry);
        X.push_back(a[i].lx);
        Y.push_back(a[i].ly);
        X.push_back(a[i].rx);
        Y.push_back(a[i].ry);
    }
    sort(X.begin(), X.end());
    X.erase(unique(X.begin(), X.end()), X.end());
    sort(Y.begin(), Y.end());
    Y.erase(unique(Y.begin(), Y.end()), Y.end());
    for(int i = 1; i <= n; i++){
        a[i].lx = lower_bound(X.begin(), X.end(), a[i].lx) - X.begin()+10;
        a[i].rx = lower_bound(X.begin(), X.end(), a[i].rx) - X.begin()+10;
        a[i].ly = lower_bound(Y.begin(), Y.end(), a[i].ly) - Y.begin()+10;
        a[i].ry = lower_bound(Y.begin(), Y.end(), a[i].ry) - Y.begin()+10;
    }/**/
}
int main(){ 
    int i, j;
    while(scanf("%d",&n), n){
        input();
        memset(mp, 0, sizeof mp);
        memset(vis, 0, sizeof vis);
        for(i = 1; i <= n; i++)
            for(j = a[i].lx; j < a[i].rx; j++)
                for(int k = a[i].ry; k< a[i].ly; k++)
                    mp[j][k] = mp[j][k] | (1ll<<i);
        int ans = 0;
        for(i = 0; i < 200; i++)
            for(j = 0; j < 200; j++)
                if(!vis[i][j])
                {
                    ans++;
                    bfs(i,j);
                }
        cout<<ans<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值