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> # ...
随机推荐
- python的re模块一些方法 && Tkinter图形界面设计 && 终止python运行函数 && python读写文件 && python一旦给字符串赋值就不能单独改变某个字符,除非重新给变量赋值
Tkinter图形界面设计见:https://www.cnblogs.com/pywjh/p/9527828.html#radiobutton 终止python运行函数: 采用sys.exit(0)正 ...
- hdu1004 Let the Balloon Rise
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- httprunner(11)运行测试报告
前言 受益于pytest的集成,HttpRunner v3.x可以使用pytest所有插件,包括pytest-html和allure-pytest,也可以实现这2种方式的报告 内置html报告 pyt ...
- k8s二进制部署 - coredns安装
coredns的资源清单文件rabc.yaml apiVersion: v1 kind: ServiceAccount metadata: name: coredns namespace: kube- ...
- Cobbler服务引导第三方PE系统
通过Cobbler服务引导第三方PE系统 1.上传第三方ios到/root/Ushendu_win10.iso并增加菜单项 cobbler distro add --name=Ushendu_win1 ...
- Nginx基础 - HTTPS安全web服务
1.HTTPS配置语法 Syntax: ssl on | off; Default: ssl off; Context: http, server Syntax: ssl_certificate fi ...
- 牛客网-Beauty of Trees 【加权并查集】
锟斤拷锟接o拷https://www.nowcoder.com/acm/contest/119/A锟斤拷源锟斤拷牛锟斤拷锟斤拷 锟斤拷目锟斤拷锟斤拷 It锟斤拷s universally acknow ...
- Leetcode(28)-实现strStr()
实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...
- 强网杯 2019]随便注(堆叠注入,Prepare、execute、deallocate)
然后就是今天学的新东西了,堆叠注入. 1';show databases; # 1';show tables; # 发现两个表1919810931114514.words 依次查询两张表的字段 1'; ...
- Docker & Node.js
Docker & Node.js https://nodejs.org/en/docs/guides/nodejs-docker-webapp/ https://docs.docker.com ...