HDOJ-6665(离散化+DFS求连通分量)
Calabash and Landlord
HDOJ-6665
- 这里考察的是离散化的知识。
- 首先将所有的x坐标和y坐标放入两个数组中,然后对这两个数组进行排序。因为总共的坐标数就5个所以这两个数组的大小只需要5就可以了(从1开始)。
- 然后利用lower_bound函数查找每一个点的横纵坐标的位置。这样就可以将点对应起来。
最后使用dfs查找图中有多少个连通分量就可以了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int sx[5],tx[5];
int sy[5],ty[5];
int disx[5],disy[5];
bool vis[10][10];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
bool in(int x,int y){
return x>0&&x<10&&y>0&&y<10;
}
void dfs(int x,int y){
vis[x][y]=1;
for(int i=0;i<4;i++){
int ex=x+dir[i][0];
int ey=y+dir[i][1];
if(in(ex,ey)){
if(!vis[ex][ey]){
dfs(ex,ey);
}
}
}
}
int main(){
int t;
cin>>t;
while(t--){
memset(vis,0,sizeof(vis));
int x,y;
for(int i=1;i<=4;i++){
cin>>x>>y;
tx[i]=sx[i]=x,ty[i]=sy[i]=y;
}
sort(tx+1,tx+5);
sort(ty+1,ty+5);
for(int i=1;i<=4;i++){
disx[i]=lower_bound(tx+1,tx+5,sx[i])-tx;//保证大于等于1但是小于5
disy[i]=lower_bound(ty+1,ty+5,sy[i])-ty;
disx[i]*=2;
disy[i]*=2;
}
for(int tex=disx[1];tex<=disx[2];tex++){
vis[tex][disy[1]]=vis[tex][disy[2]]=1;
}
for(int tex=disx[3];tex<=disx[4];tex++){
vis[tex][disy[3]]=vis[tex][disy[4]]=1;
}
for(int tey=disy[1];tey<=disy[2];tey++){
vis[disx[1]][tey]=vis[disx[2]][tey]=1;
}
for(int tey=disy[3];tey<=disy[4];tey++){
vis[disx[3]][tey]=vis[disx[4]][tey]=1;
}
int ans=0;
for(int i=1;i<=9;i++){
for(int j=1;j<=9;j++){
if(!vis[i][j]){
ans++;
dfs(i,j);
}
}
}
cout<<ans<<endl;
}
return 0;
}
HDOJ-6665(离散化+DFS求连通分量)的更多相关文章
- PAT 1013 Battle Over Cities (dfs求连通分量)
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- Aizu 0531 "Paint Color" (坐标离散化+DFS or BFS)
传送门 题目描述: 为了宣传信息竞赛,要在长方形的三合板上喷油漆来制作招牌. 三合板上不需要涂色的部分预先贴好了护板. 被护板隔开的区域要涂上不同的颜色,比如上图就应该涂上5种颜色. 请编写一个程序计 ...
- UVa 572 油田(DFS求连通块)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- [C++]油田(Oil Deposits)-用DFS求连通块
[本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...
- HDU 4607 Park Visit 两次DFS求树直径
两次DFS求树直径方法见 这里. 这里的直径是指最长链包含的节点个数,而上一题是指最长链的路径权值之和,注意区分. K <= R: ans = K − 1; K > R: ans = ...
- DFS入门之二---DFS求连通块
用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...
- UVA 572 Oil Deposits油田(DFS求连通块)
UVA 572 DFS(floodfill) 用DFS求连通块 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format: ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- hdu 1213 求连通分量(并查集模板题)
求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...
随机推荐
- int和longlong的范围
unsigned int 0-4294967295 (10位数,4e9) int -2147483648-2147483647 (10位 ...
- HDU3544 Alice's Game && POJ 2960 S-Nim(SG函数)
题意: 有一块xi*Yi的矩形巧克力,Alice只允许垂直分割巧克力,Bob只允许水平分割巧克力.具体来说,对于Alice,一块巧克力X i * Y i,只能分解成a * Y i和b * Y i其中a ...
- IntelliJ IDEA 运行java程序时出现“程序发生找不到或无法加载主类 cn.test1.test1”错误
在你程序不出现错误,而且你的编译器已经成功导入后 成功导入的样子 你可以重新打开一个项目 这就可以了^_^
- Codeforces Round #669 (Div. 2) A. Ahahahahahahahaha (构造)
题意:有一个长度为偶数只含\(0\)和\(1\)的序列,你可以移除最多\(\frac{n}{2}\)个位置的元素,使得操作后奇数位置的元素和等于偶数位置的元素和,求新序列. 题解:统计\(0\)和\( ...
- 树状数组 && 板子
本文树状数组讲解转载于:https://www.cnblogs.com/xenny/p/9739600.html 本文新加内容为模板代码部分 1.什么是树状数组? 顾名思义,就是用数组来模拟树形结构呗 ...
- Linux 驱动框架---设备文件devfs
设备文件系统 Linux引入了虚拟文件系统,从而使设备的访问可以像访问普通文件系统一样.因此在内核中描述打开文件的数据inode中的rdev成员用来记录设备文件对应到的设备号.设备文件也由一个对应的f ...
- Ubuntu 18.04 + pip3 install virtualenvwrapper 找不到virtualenvwrapper.sh
Reference Ubuntu 18.04 只自带python3.6.5, 因此不想装python2了, 但通过apt install 装virtualenvwrapper时发现必须得装python ...
- Ubuntu16.04+wineQQ+解决版本过低
[参考1:] http://blog.csdn.net/sinat_32079337/article/details/72771078? [参考2:] http://blog.csdn.net/qq_ ...
- React Security Best Practices All In One
React Security Best Practices All In One Default XSS Protection with Data Binding Dangerous URLs Ren ...
- RESTful 架构 && RESTful API
RESTful 架构 && RESTful API REpresentational State Transfer (REST) 具象状态传输https://en.wikipedia. ...