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个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...
随机推荐
- (旧)子数涵数·C语言——让C帮你做计算
之前,我们学过了我们的第一个C程序--hello World.现在开始进一步学习,想一想如何让C帮你做计算. 我们先来看代码(我没有新建,还是用之前的hello world.cpp): 好,因为之前在 ...
- [Xamarin.Android] 发布NuGet套件
[Xamarin.Android] 发布NuGet套件 前言 在Xamarin中,可以将自己开发的项目包装成为NuGet套件发布至NuGet Server,来提供其他开发人员使用.本篇介绍如何封装并发 ...
- elasticsearch分词插件的安装
IK简介 IK Analyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包.从2006年12月推出1.0版开始, IKAnalyzer已经推出了4个大版本.最初,它是以开源项目Luen ...
- RHEL7软件包管理
本文介绍RHEL7的软件包管理 RHEL7下主要有RPM和YUM这两种包管理: YUM使用简单但需要联网,YUM会去网上的YUM包源去获取所需要的软件包并获取该包依赖的其他包 RPM的需要的操作精度比 ...
- Windows环境下利用github快速配置git环境
在windows环境下利用github客户端我们可以直接拥有可视化的界面来管理工程,当然你也可以选择你喜欢的命令行工具来做.今天我分享一个比较快速的方式来配置git环境. 先去下载github的win ...
- UITabBarItem的selectedImage
TabBar使用频率很高的一个组件,TabBar的TabBarItem有两个属性一个是image(未选中图片),另一个是selectedImage(选中时图片) 但是运行时发现,选中时的图片变成了蓝色 ...
- iOS远程推送之友盟Push
更新记录: 1.2015年10月23日上午10:10分更新,优化了该类,去除了不必要的方法. ----------------------------------------------------- ...
- iOS设计模式之备忘录模式
备忘录模式 基本理解 这个模式有三个关键角色:原发器(Originator).备忘录(Memento).看管人(caretaker).三者的基本关系是:原发器创建一个包含其状态的备忘录,并传给看管人. ...
- iOS 编译报错
Undefined symbols for architecture i386: “_OBJC_CLASS_$_XXX”, referenced from: objc-class-ref in XXX ...
- QA:Failed to deploy artifacts from/to snapshots XX Failed to transfer file Return code is: 405, ReasonPhrase:Method Not Allowed.
QA: Failed to deploy artifacts from/to snapshots XX Failed to transfer file Return code is: 405, Rea ...