UVALive 6663 Count the Regions --离散化+DFS染色
题意:给你n(n<=50)个矩形(左上角坐标和右下角坐标),问这些矩形总共将平面分成多少个部分。坐标值可能有1e9.
分析:看到n和坐标的范围,容易想到离散化,当时就没想到离散化以后怎么判断区域个数。后来看别人代码才知道,可以将边界上的点vis赋为1,那么枚举所有哈希后的平面上的点,遇到一个vis=0的点就从这点一直搜过去,搜到边界自动会停止了,因为边界vis=1。所以每次看到一个vis=0的点就ans++,就可以了。真是太弱。。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
#define N 207 vector<int> vx,vy;
map<int,int> hx,hy;
int dx[] = {,,,-};
int dy[] = {,-,,};
int vis[N][N];
int l[],r[],t[],b[]; bool OK(int nx,int ny)
{
if(nx >= && nx <= && ny >= && ny <= && !vis[nx][ny])
return ;
return ;
} void dfs(int x,int y)
{
vis[x][y] = ;
for(int k=;k<;k++)
{
int nx = x + dx[k];
int ny = y + dy[k];
if(OK(nx,ny))
dfs(nx,ny);
}
} int main()
{
int n,i,j;
while(scanf("%d",&n)!=EOF && n)
{
vx.clear();vy.clear();
hx.clear();hy.clear();
for(i=;i<n;i++)
{
scanf("%d%d%d%d",&l[i],&t[i],&r[i],&b[i]);
vx.push_back(l[i]);
vx.push_back(r[i]);
vy.push_back(t[i]);
vy.push_back(b[i]);
}
sort(vx.begin(),vx.end());
vx.erase(unique(vx.begin(),vx.end()),vx.end()); //运用STL巧妙去重
sort(vy.begin(),vy.end());
vy.erase(unique(vy.begin(),vy.end()),vy.end());
for(i=;i<vx.size();i++) //以2为间隔,防止出现面积为1的小矩形计算不到的情况
hx[vx[i]] = *i+;
for(i=;i<vy.size();i++)
hy[vy[i]] = *i+;
for(i=;i<n;i++) //离散后的值
{
l[i] = hx[l[i]];
t[i] = hy[t[i]];
r[i] = hx[r[i]];
b[i] = hy[b[i]];
}
memset(vis,,sizeof(vis));
for(i=;i<n;i++) //边界赋值
{
for(j=b[i];j<=t[i];j++)
{
vis[j][l[i]] = ;
vis[j][r[i]] = ;
}
for(j=l[i];j<=r[i];j++)
{
vis[t[i]][j] = ;
vis[b[i]][j] = ;
}
}
int cnt = ;
for(i=;i<=;i++)
{
for(j=;j<=;j++)
{
if(!vis[i][j])
{
cnt++;
dfs(i,j);
}
}
}
printf("%d\n",cnt);
}
return ;
}
UVALive 6663 Count the Regions --离散化+DFS染色的更多相关文章
- UvaLive 6663 Count the Regions 离散化+DFS
链接:http://vjudge.net/problem/viewProblem.action?id=49408 题意:在平面内给出若干个矩形,求出它们能将整个平面分成多少份. 思路:刚開始一眼看到认 ...
- UVALive 6663 Count the Regions 离散+bfs染色_(:зゝ∠)_
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4675">点击打开链接 gg.. ...
- [UVALive 6663 Count the Regions] (dfs + 离散化)
链接:https://icpcarchive.ecs.baylor.edu/index.php? option=com_onlinejudge&Itemid=8&page=show_p ...
- 训练指南 UVALive - 3523 (双联通分量 + 二分图染色)
layout: post title: 训练指南 UVALive - 3523 (双联通分量 + 二分图染色) author: "luowentaoaa" catalog: tru ...
- hdu 4751(dfs染色)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...
- hdu 5313 Bipartite Graph(dfs染色 或者 并查集)
Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...
- hdu 4751 Divide Groups(dfs染色 或 2-sat)
Problem Description This year is the 60th anniversary of NJUST, and to make the celebration more c ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)
题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...
随机推荐
- Follow me to learn what is Unit of Work pattern
Introduction A Unit of Work is a combination of several actions that will be grouped into a transact ...
- SQLServer处理行转列和列转行
掌握SQL Server 行转列和列转行 1.列转行 数据经过计算加工后会直接生成前端图表需要的数据源,但是程序里又需要把该数据经过列转行写入中间表中,下次再查询该数据时直接从中间表查询数据. 1.1 ...
- 【使用 DOM】使用事件
1. 使用简单事件处理器 可以用几种不同的方式处理事件.最直接的方式是用事件属性创建一个简单事件处理器(simple event handler).元素为它们支持的每一种事件都定义了一个事件属性.举个 ...
- SAP技术相关Tcode
ABAP的常用tcode 开发----------------------------------------------- SE51 屏幕制作 SE91 MESSAGE OBJECT SE80 ...
- Atitit.视频文件加密的方法大的总结 java c# php
Atitit.视频文件加密的方法大的总结 java c# php 1. 加密的算法 aes 3des des xor等.1 2. 性能1 3. 解密1 4. 播放器的事件扩展1 5. 自定义格式 ...
- Echarts ecomfe 触摸屏 touch 在IE10下无法显示悬浮框
问题描述: Windows 8 IE10浏览http://echarts.baidu.com/doc/example/line2.html 时,鼠标放置在数据点上时无法显示悬浮框. 正常情况为: 而现 ...
- iOS多线程初见
. 三种创建线程的方法 //第一种 NSThread * thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(doAc ...
- 【转】c++中Vector等STL容器的自定义排序
如果要自己定义STL容器的元素类最好满足STL容器对元素的要求 必须要求: 1.Copy构造函数 2.赋值=操作符 3.能够销毁对象的析构函数 另外: 1. ...
- android 进程/线程管理(三)----Thread,Looper / HandlerThread / IntentService
Thread,Looper的组合是非常常见的组合方式. Looper可以是和线程绑定的,或者是main looper的一个引用. 下面看看具体app层的使用. 首先定义thread: package ...
- mysql字段不能为空的字段为空时也能插入的方法
接手了一个项目,设计数据库的时候字段全部是不能为空,但是空值又可以插入数据,刚拿过来的时候就提示各种sql语法错误,现记录一下解决办法. 将my.ini中设置: #sql-mode=STRICT_TR ...