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

[UVALive 6663 Count the Regions] (dfs + 离散化)的更多相关文章

  1. UVALive 6663 Count the Regions --离散化+DFS染色

    题意:给你n(n<=50)个矩形(左上角坐标和右下角坐标),问这些矩形总共将平面分成多少个部分.坐标值可能有1e9. 分析:看到n和坐标的范围,容易想到离散化,当时就没想到离散化以后怎么判断区域 ...

  2. UvaLive 6663 Count the Regions 离散化+DFS

    链接:http://vjudge.net/problem/viewProblem.action?id=49408 题意:在平面内给出若干个矩形,求出它们能将整个平面分成多少份. 思路:刚開始一眼看到认 ...

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

    题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4675">点击打开链接 gg.. ...

  4. DFS染色解决区域分块问题UVALive 6663

    怪我比赛的时候想法太过于杂乱了. 注重于区域的属性了.甚至还想用状态压缩或者是hash来描述分块的区域. 其实我们的可以宏观的角度去审视这个问题.就是求分区的问题.那么我们完全可以标记边框的值为1.即 ...

  5. HDU 5877 Weak Pair(树状数组+dfs+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=5877 题意: 给出一棵树,每个顶点都有权值,现在要你找出满足要求的点对(u,v)数,u是v的祖先并且a[u]*a ...

  6. UVALive - 6436、HYSBZ - 2435 (dfs)

    这两道题都是用简单dfs解的,主要是熟悉回溯过程就能做,据说用bfs也能做 道路修建(HYSBZ - 2435) 在 W 星球上有n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道 ...

  7. Leetcode 130 Surrounded Regions DFS

    将内部的O点变成X input X X X XX O O X X X O XX O X X output X X X XX X X XX X X XX O X X DFS的基本框架是 void dfs ...

  8. UVALive 6884 GREAT + SWERC = PORTO dfs模拟

    题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  9. UVALive 7334 Kernel Knights (dfs)

    Kernel Knights 题目链接: http://acm.hust.edu.cn/vjudge/contest/127407#problem/K Description Jousting is ...

随机推荐

  1. 【Unity 3D】学习笔记四十:射线

    射线 射线,类比的理解就是游戏中的子弹.是在3D世界里中一个点向一个方向发射的一条无终点的线.在发射的过程中,一旦与其它对象发生碰撞,就停止发射. 射线的原理 创建一个射线时,首先须要知道射线的起点和 ...

  2. urlrewrite 匹配规则之优先选择

    urlrewrite rule可以使用java的正则表达式匹配规则,但是这里存在一个问题点,假如有一个通配的规则和一个精确匹配的规则,urlrewrite 会选择那个去匹配呢? 如下两种规则: < ...

  3. Python 绝对简明手册

    Python 绝对简明手册 help(函数名)来获取相关信息 另外,自带的文档和google也是不可少的 2. 基本语法2.1. if / elif / else x=int(raw_input(&q ...

  4. 新鲜出炉的Using Qt 3D to visualize music

    http://blog.qt.io/blog/2016/01/27/using-qt-3d-visualize-music/

  5. Mysql事务,并发问题,锁机制-- 幻读、不可重复读(转)

    1.什么是事务 事务是一条或多条数据库操作语句的组合,具备ACID,4个特点. 原子性:要不全部成功,要不全部撤销 隔离性:事务之间相互独立,互不干扰 一致性:数据库正确地改变状态后,数据库的一致性约 ...

  6. mysql Emoji表情字符集转换

    <pre name="code" class="html">Java代码 java.sql.SQLException: Incorrect stri ...

  7. scrapy新浪天气

    一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu Linux环境,实验中会用到桌面上的程序: LX终端(LXTermin ...

  8. 基于visual Studio2013解决面试题之1003字符串逆序

     题目

  9. [linux]linux命令学习-netstat

    linux非常多服务都与网络相关.当服务调不通或者是启动port被占用,或者是又是被防火墙挡住的时候,就须要查询网络相关的问题,netstat命令之前仅仅会用一两个參数这里.好好学习一番. 经常使用的 ...

  10. Python爬虫入门三之Urllib库的基本使用

    转自http://cuiqingcai.com/947.html 1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由 ...