[UVALive 6663 Count the Regions] (dfs + 离散化)[通俗易懂]

[UVALive 6663 Count the Regions] (dfs + 离散化)

大家好,又见面了,我是全栈君。

链接:https://icpcarchive.ecs.baylor.edu/index.php?

option=com_onlinejudge&Itemid=8&page=show_problem&problem=4675

题目大意:

在一个平面上有 n (1<=n<=50) 个矩形。给你左上角和右下角的坐标(0<=x<=10^6, 0<=y<=10^6)。问这些矩形将该平面划分为多少块。

解题思路:

因为n非常小,能够对整个图进行压缩。仅仅要不改变每条边的相对位置。对答案没有影响。

能够将这些矩形的坐标离散化,然后把边上的点标记一下。之后进行简单dfs就可以。(注意离散化的时候,两条边之间至少要隔一个距离)

代码:

/*
ID: wuqi9395@126.com
PROG:
LANG: C++
*/
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<fstream>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<algorithm>
#define INF (1<<30)
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
#define For(i, n) for (int i = 0; i < n; i++)
using namespace std;
const int MOD = 1000000007;
typedef long long ll;
using namespace std;
struct node {
    int a, b, c, d;
} rec[600];
int x[1200], y[1200];
int xp[1000100], yp[1000100];
int mp[240][240], n;
int lx, ly;
void gao() {
    for (int i = 0; i < n; i++) {
        int A = xp[rec[i].a];
        int B = yp[rec[i].b];
        int C = xp[rec[i].c];
        int D = yp[rec[i].d];
        for (int j = A; j <= C; j++) mp[j][D] = mp[j][B] = 1;
        for (int j = D; j <= B; j++) mp[A][j] = mp[C][j] = 1;
    }
}
int dir[4][2] = {{0, -1}, {0, 1}, {1, 0}, { -1, 0}};
bool in(int x, int y) {
    return (x >= 0 && y >= 0 && x < 2 * lx + 1 && y < 2 * ly + 1);
}
void dfs(int x, int y) {
    mp[x][y] = 1;
    for (int i = 0; i < 4; i++) {
        int xx = x + dir[i][0];
        int yy = y + dir[i][1];
        if (in(xx, yy) && !mp[xx][yy]) {
            dfs(xx, yy);
        }
    }
}
int main() {
    while(scanf("%d", &n) != EOF && n) {
        for (int i = 0; i < n; i++) {
            scanf("%d%d%d%d", &rec[i].a, &rec[i].b, &rec[i].c, &rec[i].d);
            x[2 * i] = rec[i].a;
            x[2 * i + 1] = rec[i].c;
            y[2 * i] = rec[i].b;
            y[2 * i + 1] = rec[i].d;
        }
        sort(x, x + 2 * n);
        sort(y, y + 2 * n);
        lx = unique(x, x + 2 * n) - x;
        ly = unique(y, y + 2 * n) - y;
        for (int i = 0; i < lx; i++) {
            xp[x[i]] = 2 * i + 1;
        }
        for (int j = 0; j < ly; j++) {
            yp[y[j]] = 2 * j + 1;
        }
        memset(mp, 0, sizeof(mp));
        gao();
        int fk = 0;
        for (int i = 0; i < 2 * lx; i++) {
            for (int j = 0; j < 2 * ly; j++) if (mp[i][j] == 0) {
                    dfs(i, j);
                    fk++;
                }
        }
        cout << fk << endl;
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116289.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux-shell脚本命令之sed

    linux-shell脚本命令之sed

    2021年12月8日
    54
  • linux redis命令客户端,Redis客户端

    linux redis命令客户端,Redis客户端Redis客户端Redis客户端是一个程序,通过网络连接到Redis服务器,在客户端软件中使用Redis可以识别的命令,向Redis服务器发送命令,告诉Redis想要做什么。Redis把处理结果显示在客户端界面上。通过Redis客户端和Redis服务器交互。Redis客户端发送命令,同时显示Redis服务器的处理结果在Redis命令行客户端redis-cli…

    2022年5月1日
    67
  • no information is available_no data available什么意思

    no information is available_no data available什么意思解决方案一、打开魔术棒二、output→BrowseInfoemation三、重新编译就可以了

    2022年9月18日
    2
  • 客户信息管理系统_销售找客户最好的app

    客户信息管理系统_销售找客户最好的app客户信息管理系统课程设计的题目及简介设计说明程序流图程序清单Customer类MainView类Tools类DataManager类调试结果课程设计体会课程设计的题目及简介客户信息管理系统,功能如下:(1)添加客户信息(2)修改客户信息(3)删除客户数据(4)查询客户列表(5)所有数据通过JDBC保存到MySql数据库中1,数据库名:cms_hisoft2,表名:users3,字段列表和类型:id,int,主键,自动增长name,varchar(20),姓名gender,var

    2022年10月17日
    3
  • 解决github下载慢及–recursive慢的问题(亲测好用)[通俗易懂]

    在gitclone的地址,例如https://github.com/pytorch/pytorch,改为https://gitclone.com/github.com/pytorch/pytorch,也即加上前缀gitclone.com,然后就可以愉快的下载了(亲测有效)。对于子模块,可以先不要在gitclone的时候加上–recursive,等主体部分下载完之后,该文件夹中有个隐藏文件称为:.gitmodules,把子项目中的url地址同样加上gitclone.com前缀,然后利用gits..

    2022年4月12日
    82
  • 矩阵论(1)三种常见的矩阵范数

    矩阵论(1)三种常见的矩阵范数总结了三种矩阵范数:1-范数,2-范数以及∞-范数。

    2022年9月19日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号