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> # ...
随机推荐
- Codeforces Round #579 (Div. 3) B Equal Rectangles、C. Common Divisors
B Equal Rectangles 题意: 给你4*n个数,让你判断能不能用这个4*n个数为边凑成n个矩形,使的每个矩形面积相等 题解: 原本是想着用二分来找出来那个最终的面积,但是仔细想一想,那个 ...
- Java 窗口 绘制图形 #3
写在前面: 高数下学到第二章,突发奇想要写一个程序画二元函数图像 思路分了三层: ①抽象层: 因变量z,自变量x.y,坐标原点x0.y0.z0 ②投影实现层: 屏幕投影坐标px.py,x轴与屏幕水平方 ...
- [POJ 2585] Window Pains 拓朴排序
题意:你现在有9个2*2的窗口在4*4的屏幕上面,由于这9这小窗口叠放顺序不固定,所以在4*4屏幕上有些窗口只会露出来一部分. 如果电脑坏了的话,那么那个屏幕上的各小窗口叠放会出现错误.你的任务就是判 ...
- Codeforces Round #635 C. Linova and Kingdom
传送门:C. Linova and Kingdom 题意:给你一棵树,要求对k个结点涂色,然后统计每个未涂色结点到根结点的路径上未涂色结点的和,求和最大能为多少 题解:对着样例画几遍,然后贪心发现,最 ...
- Qt开发Activex笔记(二):Qt调用Qt开发的Activex控件
若该文为原创文章,转载请注明原文出处本文章博客地址:https://blog.csdn.net/qq21497936/article/details/113789693 长期持续带来更多项目与技术分享 ...
- JPG学习笔记3(附完整代码)
#topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, ...
- Apple iOS 触控按钮 自动关闭 bug
Apple iOS 触控按钮 自动关闭 bug bug 轻点 iPhone 背面可执行操作 您可以轻点两下或轻点三下 iPhone 背面以执行某些操作,如向上或向下滚动.截屏.打开"控制中心 ...
- how to import a SQL file in MySQL command line
how to import a SQL file in MySQL command line execute .sql file, macOS $mysql> source \home\user ...
- Flutter 1.17.x
Flutter 1.17.x Flutter (Channel stable, v1.17.3, on Mac OS X 10.15.5 19F101, locale en-CN) https://f ...
- asm align 对齐数据
最大成员dword data: dd 1 db 2 align 4 dw 3 000E0010 - 01 00 00 00 000E0014 - 02 00 00 00 000E0018 - 03 0 ...