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个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...
随机推荐
- mongodb学习3---mongo的MapReduce
1,概述MapReduce是个非常灵活和强大的数据聚合工具.它的好处是可以把一个聚合任务分解为多个小的任务,分配到多服务器上并行处理.MongoDB也提供了MapReduce,当然查询语肯定是Java ...
- 动态创建JS
var element=document.createElement('script'); element.setAttribute('src', './js/move.js'); document. ...
- Linux下建立Nexus私服
Linux下建立Nexus私服 要安装3个东西,然后配置私服: 1.JDK 2.Maven 3.Nexus 然后配置 1.JDK的安装 下载JDK安装包,格式为RPM格式,安装即可 安装程序 #rpm ...
- 【GOF23设计模式】策略模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_策略模式.CRM中报价策略.GUI编程中布局管理器底层架构 package com.test.strategy; /** ...
- QT4/5中文乱码问题解决
QT4 : QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); QT5: #if defined(_MSC_ ...
- GridControl列自动匹配宽度
//自动调整所有字段宽度this.gridView1.BestFitColumns(); //调整某列字段宽度this.gridView1.Columns[n].BestFit(); 大多是网上零散找 ...
- gridView使用
只读 for (int i = 0; i <9; i++) { this.gridView1.Columns[i].OptionsColumn.ReadOnly = true; } 不显示面板 ...
- 用R语言分析我的fitbit计步数据
目标:把fitbit的每日运动记录导入到R语言中进行分析,画出统计图表来 已有原始数据:fitbit2014年每日的记录电子表格文件,全部数据点此下载,示例如下: 日期 消耗卡路里数 步 距离 攀爬楼 ...
- git学习笔记1
很早以前就听说了git,今天就开始使用git,并做简单记录 在Linux上安装Git 首先,你可以试着输入git,看看系统有没有安装Git: $ git The program 'git' is cu ...
- bash shell命令(1)
本文地址:http://www.cnblogs.com/archimedes/p/bash-shell1.html,转载请注明源地址. ls命令 ls用来列出目录的内容,它是用户最常用的命令之一,ls ...