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

本文介绍了如何解决UVALive 6663数模问题,该问题涉及平面内n个矩形的划分。通过离散化坐标和使用深度优先搜索(dfs),可以有效地计算出矩形将平面分割成的区域数量。解题策略是先对所有矩形的坐标进行离散化,确保不改变边的相对位置,接着标记边上的点,最后执行dfs遍历来确定区域计数。

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

链接: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;
}


TCUTTER - Tin Cutter In a Tin Cutting factory there is a machine for cutting parts from tin plates. It has an extraordinarily sharp knife able to make horizontal or vertical segment cuts in the tin plates. Each cutting process consists of a sequence of such cuts. Each segment cut is given by its endpoints that are always located inside the tin plate. During the cutting process some parts of tin plate can fall out and so some holes in the plate can emerge. Factory management needs to predict the number of holes in the plate at the end of the given sequence of cuts. Write a program that answers this question. Single segment cuts are not considered to be holes. Here there are examples of some situations that can arise after cutting: two holes two holes one hole one hole Input The input file consists of blocks of lines. Each block except the last describes one cutting process. In the first line of the block there is a number N <= 100. indicating the number of segment cuts in the cutting process. These cuts are defined by the following N lines. The line defining one segment cut has the form X1 Y1 X2 Y2 where X1 Y1 and X2 Y2 are the co-ordinates of the end points of the segment cut. They are separated by one space. The co-ordinates are integers and always define horizontal or vertical segment (i.e. segment parallel with x or y axis). The last block consists of just one line containing 0. Output The output file contains the lines corresponding to the blocks in the input file. Each such line contains the number of holes that remain in the tin plate after the execution of the corresponding cuts. There is no line in the output file corresponding to the last "null" block of the input file. Example Input: 4 0 1 1 1 1 1 1 0 1 0 0 0 0 0 0 1 2 0 1 2 1 1 2 1 0 0 Output: 1 0
最新发布
07-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值