[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)
上一篇 2022年1月23日 下午2:00
下一篇 2022年1月23日 下午2:00


相关推荐

  • 杨植麟合写的技术报告来了!Kimi K2也是作者,还拿了24项开源SOTA

    杨植麟合写的技术报告来了!Kimi K2也是作者,还拿了24项开源SOTA

    2026年3月12日
    1
  • matlab画心形曲线_笛卡尔心形曲线方程

    matlab画心形曲线_笛卡尔心形曲线方程MATLAB心形曲线基本知识clc;指令可以清除屏幕,所以你可以通过clc指令clcholdon;指令可以将画的图连起来holdon第一种实现方式cleart=-pi:pi/100:pi;r=abs(t);x=r.*sin(t);y=r.*cos(t);plot(x,y)title(‘Iloveyou.’)axisequal…

    2022年10月17日
    5
  • 使用burpsuite抓包和改包[通俗易懂]

    使用burpsuite抓包和改包[通俗易懂]第一次使用到这个工具,是在上web安全课的时候,老师让我们进行CTF实验,采用burpsuite进行抓包改包,才发现这个工具的强大。1burpsuite工具下载官网链接:https://portswigger.net/burp/下载之后直接安装即可,比较简单2建立burpsuite和浏览器的连接打开burpsuite工具,在proxy中的Option下,看到对应的Interface…

    2022年6月1日
    228
  • 制作ext4文件系统

    制作ext4文件系统原文地址 https blog csdn net zuoyioo7 article details 制作 ext4 文件系统 beaglebone 对应硬件设备存储器均为 emmc 并不是传统的 nandflash 因此文件系统格式也不再是传统的 yaffs 或是 jffs 文件系统了 需要 ext4 文件系统 在 debian 用户目录下新建目录 rootfs tmp 文件

    2026年3月20日
    2
  • 在pycharm中配置Anaconda以及pip源配置

    在pycharm中配置Anaconda以及pip源配置在学习推荐系统、机器学习、数据挖掘时,python是非常强大的工具,也有很多很强大的模块,但是模块的安装却是一件令人头疼的事情。现在有个工具——anaconda,他已经帮我们集成好了很多工具了!anaconda里面集成了很多关于python科学计算的第三方库,主要是安装方便,而python是一个编译器,如果不使用anaconda,那么安装起来会比较痛苦,各个库之间的依赖性就很难连接的很好。在wind

    2022年5月15日
    39
  • Cocos2d-x Box2D物理引擎编译设置

    Cocos2d-x Box2D物理引擎编译设置

    2022年1月15日
    48

发表回复

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

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