hdu 1198 (并查集 or dfs) Farm Irrigation
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198
有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇水,问需要打多少口井
比如
ADC
FJK
IHE是下图这种情况

需要打三口井,其实想多了就是集合合并差不多,用并查集可以解决,将每个格子按顺序是从0到n*m-1,然后根据连接情况判断是否相连然后合并
第一次写成了暴力的,虽然一遍过了,但是觉得太长了,然后又改成了下面简洁并查集的,还有下下面的dfs的
并查集
#include<cstdio>
using namespace std;
char yj[][];
int father[*+];
int dir[][]={{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,},
{,,,},{,,,}};
void give(int x)
{
for (int i=;i<x;i++)
father[i]=i;
}
int _find(int x)
{
if (x==father[x]) return father[x];
father[x]=_find(father[x]);
return father[x];
}
int merge(int x,int y)
{
int sx=_find(x);
int sy=_find(y);
if (sx!=sy)
father[sx]=sy;
}
int main()
{
int n,m,i,j;
while (~scanf("%d %d",&n,&m))
{
if (n==-&&m==-) break;
for (i=;i<n;i++)
scanf("%s",yj[i]);
give(n*m);
for (i=;i<n;i++)
{
for (j=;j<m;j++)
{
int x=yj[i][j]-'A';
int y=yj[i][j+]-'A';
if (dir[x][]==&&dir[y][]==&&j+<m)
merge(i*m+j,i*m+j+);
y=yj[i+][j]-'A';
if (dir[x][]==&&dir[y][]==&&i+<n)
merge(i*m+j,(i+)*m+j);
}
}
int sum=;
for (i=;i<n*m;i++)
if (father[i]==i)
sum++;
printf("%d\n",sum);
}
return ;
}
dfs
#include<cstdio>
#include<cstring>
using namespace std;
int dx[]={,-,,};//下上左右
int dy[]={,,-,};
char yj[][];
int ans,vis[][],n,m;
int dir[][]={{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,},
{,,,},{,,,}};
int check(int x,int y,int di)
{
if (dir[x][]&&dir[y][]&&di==) return ;
if (dir[x][]&&dir[y][]&&di==) return ;
if (dir[x][]&&dir[y][]&&di==) return ;
if (dir[x][]&&dir[y][]&&di==) return ;
return ;
}
void dfs(int x,int y)
{
int i;
if (vis[x][y]) return;
vis[x][y]=;
//printf("%d %d\n",x,y);
for (i=;i<;i++)
{
int sx=x+dx[i];
int sy=y+dy[i];
if (sx<||sx>=n||sy<||sy>=m) continue;
if (vis[sx][sy]) continue;
int q=yj[sx][sy]-'A';
int w=yj[x][y]-'A';
if (check(q,w,i))
dfs(sx,sy);
}
}
int main()
{
int i,j;
while (~scanf("%d %d",&n,&m))
{
if (n==-&&m==-) break;
for (i=;i<n;i++)
scanf("%s",yj[i]);
memset(vis,,sizeof(vis));
ans=;
for (i=;i<n;i++)
{
for (j=;j<m;j++)
{
if (vis[i][j]) continue;
dfs(i,j);
ans++;
}
}
printf("%d\n",ans);
}
return ;
}
hdu 1198 (并查集 or dfs) Farm Irrigation的更多相关文章
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 3926 并查集 图同构简单判断 STL
给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...
- HDU 4496 并查集 逆向思维
给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- hdu-1272 小希的迷宫---并查集或者DFS
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1272 题目大意: Problem Description 上次Gardon的迷宫城堡小希玩了很久(见 ...
- HDU 2860 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...
- POJ 1562 Oil Deposits (并查集 OR DFS求联通块)
Oil Deposits Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14628 Accepted: 7972 Des ...
- Is It A Tree?(并查集)(dfs也可以解决)
Is It A Tree? Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submi ...
- hdu 1598 (并查集加贪心) 速度与激情
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...
随机推荐
- Android sdk 目录结构说明
1.add-on:附加的包:2.docs:HTML格式的离线文档:3.platforms:sdk核心内容:4.tool:工具. 在platforms中包含了的各个Android SDK版本的目录中,包 ...
- C# Excel导数据
遇到的几个坑,记录一下. 隐藏表,隐藏行,隐藏列, 单元格合并 => 拆分并填充内容, 显示隐藏列,结果那个列 还是不显示出来,拖动旁边的列也不显示. 结果在旁边的列上,按左右箭头键就出来了,也 ...
- 学JS的心路历程-物件与原型(三)
昨天有说明到函式与建构式的原型,及指定建构式函式原型为另一个建构式函式,但其实这会造成复写constructor的问题. 复写constructor的问题(vmwork) 我们昨天有提到「建构式函式可 ...
- 1.3.3、CDH 搭建Hadoop在安装之前(端口---CDH组件使用的端口)
列出的所有端口都是TCP. 在下表中,每个端口的“ 访问要求”列通常是“内部”或“外部”.在此上下文中,“内部”表示端口仅用于组件之间的通信; “外部”表示该端口可用于内部或外部通信. Compone ...
- Unity入门&物理引擎
一.Unity六大模块 首先,Unity界面有六大模块,分别是:Hierarchy,Scene,Game,Inspector,Project,Console.下面对这六个视图的功能进行详解. 1.Hi ...
- ICMP隧道 ptunnle
通过ICMP echo(ping request)和reply(ping reply)实现隧道 适用于防火墙只允许ping出站流量的环境 支持多并发连接,性能优 支持身份验证 使用时需要root用户 ...
- 【Django】关于ORM的使用
添加模型并映射到数据库中: 1. 在 settings.py 中,配置好 DATABASES ,做好数据库相关的配置. 以mysql为例: DATABASES = { 'default': { 'EN ...
- centos 7 redis-4.0.11 主从
redis-master:192.168.199.223 redis-slave: 192.168.199.224 cd /opt wget http://download.redis.io/rele ...
- C语言之栈区、堆区
一 局部变量存放在栈区中,函数调用结束后释放内存空间. #include "stdio.h"; #include "stdlib.h"; int *getNum ...
- [剑指Offer]54-二叉搜索树的第k个节点
题目描述 给定一棵二叉搜索树,找出其中的第k小的结点,返回指向该节点的指针. 思路 中序遍历即可. 注意特判!报段错误数组越界这里就要考虑是少特判的问题. 法一:借助vector 法二(better) ...