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求连通分量)的更多相关文章

  1. 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 ...

  2. Aizu 0531 "Paint Color" (坐标离散化+DFS or BFS)

    传送门 题目描述: 为了宣传信息竞赛,要在长方形的三合板上喷油漆来制作招牌. 三合板上不需要涂色的部分预先贴好了护板. 被护板隔开的区域要涂上不同的颜色,比如上图就应该涂上5种颜色. 请编写一个程序计 ...

  3. UVa 572 油田(DFS求连通块)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. [C++]油田(Oil Deposits)-用DFS求连通块

    [本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...

  5. HDU 4607 Park Visit 两次DFS求树直径

    两次DFS求树直径方法见 这里. 这里的直径是指最长链包含的节点个数,而上一题是指最长链的路径权值之和,注意区分. K <= R: ans = K − 1; K > R:   ans = ...

  6. DFS入门之二---DFS求连通块

    用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...

  7. UVA 572 Oil Deposits油田(DFS求连通块)

    UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format: ...

  8. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. hdu 1213 求连通分量(并查集模板题)

    求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...

随机推荐

  1. 洛谷 P1135 奇怪的电梯 (DFS)

    题意:有一\(n\)层高的大楼,每层楼都只能在合法情况下上\(x\)层或者下\(x\)层,问你是否能从\(a\)层楼坐电梯到\(b\)层楼,输出最小步数. 题解:直接dfs搜,\(flo\)表示层数, ...

  2. Ancient Printer HDU - 3460 贪心+字典树

    The contest is beginning! While preparing the contest, iSea wanted to print the teams' names separat ...

  3. 国产网络测试仪MiniSMB - 双击就可以直接编辑数据报文字段(如IP地址)

    国产网络测试仪MiniSMB(www.minismb.com)是复刻smartbits的IP网络性能测试工具,是一款专门用于测试智能路由器,网络交换机的性能和稳定性的软硬件相结合的工具.可以通过此以太 ...

  4. K8S(15)监控实战-ELK收集K8S内应用日志

    K8S监控实战-ELK收集K8S内应用日志 目录 K8S监控实战-ELK收集K8S内应用日志 1 收集K8S日志方案 1.1 传统ELk模型缺点: 1.2 K8s容器日志收集模型 2 制作tomcat ...

  5. Leetcode(878)-第 N 个神奇数字

    如果正整数可以被 A 或 B 整除,那么它是神奇的. 返回第 N 个神奇数字.由于答案可能非常大,返回它模 10^9 + 7 的结果. 示例 1: 输入:N = 1, A = 2, B = 3 输出: ...

  6. _.shuffle、_.debounce中下划线对象的理解

    Vue 官方教程中有_.shuffle._.debounce,不明白"_"是怎么来的,有什么意义? Lodash 和 Underscorejs 都有相关解释

  7. Dos简单命令及CMD打开方式

    打开CMD方式 开始+系统+命令提示符 WIN健+R 输入cmd打开控制台(推荐使用) 在任意文件夹下面,按住shift+鼠标右击,进入powershell 资源管理器的地址栏路径最前面+cmd+空格 ...

  8. git & github & git clone & 'git clone' failed with status 128

    git & github & git clone & 'git clone' failed with status 128 'git clone' failed with st ...

  9. ES Next & Arrow function & Promise & Iterator & Generator yield & Async Await

    ES Next & Arrow function & Promise & Iterator & Generator yield & Async Await co ...

  10. macOS & Xnip

    macOS & Xnip close finished notation cancel checked xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许 ...