hdu 1198 Farm Irrigation(并查集)
题意:
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot of samll squares. Water pipes are placed in these squares. Different square has a different type of pipe. There are 11 types of pipes, which is marked from A to K, as Figure 1 shows.

Benny has a map of his farm, which is an array of marks denoting the distribution of water pipes over the whole farm. For example, if he has a map
ADC
FJK
IHE
then the water pipes are distributed like

Several wellsprings are found in the center of some squares, so water can flow along the pipes from one square to another. If water flow crosses one square, the whole farm land in this square is irrigated and will have a good harvest in autumn.
Now Benny wants to know at least how many wellsprings should be found to have the whole farm land irrigated. Can you help him?
There are several test cases! In each test case, the first line contains 2 integers M and N, then M lines follow. In each of these lines, there are N characters, in the range of 'A' to 'K', denoting the type of water pipe over the corresponding square. A negative M or N denotes the end of input, else you can assume 1 <= M, N <= 50.
思路:
并查集,看代码。
代码:
struct node{
bool up,down,left,right;
}
a[2505];
int m,n;
char graph[55][55];
int fa[2505];
int findFa(int x){
return fa[x]==x?fa[x]:fa[x]=findFa(fa[x]);
}
void Union(int x,int y){
int fx=findFa(x);
int fy=findFa(y);
if(fx!=fy){
fa[fx]=fy;
}
}
int main(){
while(scanf("%d%d",&m,&n),m>0||n>0){
rep(i,0,m-1) scanf("%s",graph[i]);
rep(i,1,m*n) a[i].up=a[i].down=a[i].left=a[i].right=false;
rep(i,0,m-1){
rep(j,0,n-1){
switch(graph[i][j]){
case 'A':a[i*n+j+1].up=true; a[i*n+j+1].left=true; break;
case 'B':a[i*n+j+1].up=true; a[i*n+j+1].right=true; break;
case 'C':a[i*n+j+1].left=true; a[i*n+j+1].down=true; break;
case 'D':a[i*n+j+1].right=true; a[i*n+j+1].down=true; break;
case 'E':a[i*n+j+1].up=true; a[i*n+j+1].down=true; break;
case 'F':a[i*n+j+1].left=true; a[i*n+j+1].right=true; break;
case 'G':a[i*n+j+1].left=true; a[i*n+j+1].right=true; a[i*n+j+1].up=true; break;
case 'H':a[i*n+j+1].up=true; a[i*n+j+1].down=true; a[i*n+j+1].left=true; break;
case 'I':a[i*n+j+1].right=true; a[i*n+j+1].left=true; a[i*n+j+1].down=true; break;
case 'J':a[i*n+j+1].up=true; a[i*n+j+1].down=true; a[i*n+j+1].right=true; break;
case 'K':a[i*n+j+1].up=true; a[i*n+j+1].left=true; a[i*n+j+1].down=true; a[i*n+j+1].right=true; break;
default: break;
}
}
}
rep(i,1,m*n) fa[i]=i;
rep(i,1,m){
rep(j,1,n){
int now=(i-1)*n+j;
if(j!=1){
int last1=(i-1)*n+j-1;
if(a[last1].right && a[now].left){
Union(last1,now);
}
}
if(i!=1){
int last2=(i-2)*n+j;
if(a[last2].down && a[now].up){
Union(last2,now);
}
}
}
}
map<int,int> mp;
mp.clear();
int ans=0;
rep(i,1,m*n){
int t=findFa(i);
if(mp[t]==0){
++ans;
mp[t]=1;
}
}
printf("%d\n",ans);
}
return 0;
}
hdu 1198 Farm Irrigation(并查集)的更多相关文章
- 杭电OJ——1198 Farm Irrigation (并查集)
畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...
- hdu 1198 Farm Irrigation(深搜dfs || 并查集)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...
- HDU 1198 Farm Irrigation(并查集,自己构造连通条件或者dfs)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1198 Farm Irrigation(并查集+位运算)
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- HDU 1198 Farm Irrigation(状态压缩+DFS)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目: Farm Irrigation Time Limit: 2000/1000 MS (Ja ...
- HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu1198 Farm Irrigation 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 简单并查集 分别合并竖直方向和水平方向即可 代码: #include<iostream&g ...
- hdu.1198.Farm Irrigation(dfs +放大建图)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1198 Farm Irrigation
令人蛋疼的并查集…… 我居然做了大量的枚举,居然过了,我越来越佩服自己了 这个题有些像一个叫做“水管工”的游戏.给你一个m*n的图,每个单位可以有11种选择,然后相邻两个图只有都和对方连接,才判断他们 ...
随机推荐
- jvm运行过程
------------恢复内容开始------------ 把文件编译成字节码文件的叫编译器的前端, 线程共享的方法去和堆,非线程共享的:java虚拟机栈,本地方法栈,还有程序计数器 都是每个线程独 ...
- Java面向对象系列(6)- 封装详解
封装 该露的露,该藏得藏 我们程序设计要追求"高内聚,低耦合".高内聚就是类得内部数据操作细节自己完成,不允许外部干涉:低耦合:仅暴露少量得方法给外部使用 封装(数据得隐藏) 通常 ...
- vue中data为什么不写成data:{}这样而是写成data(){return {}}类型。
data:{}:这样会直接挂载在vue实例中,变成全局变量,容易造成污染,再次今日该组件页面,会保留上次的变量值,不会被初始化 data(){return {}} :return包裹后数据中变量只在当 ...
- 字体图标Icon Font
字体图标Icon Font 前段时间研究怎样做字体图标,在网上查找诸多资料,诸多尝试,找到一套可以自己制作自己独立控制的制作流程,公司按照这套流程形成一套自己公司图标,本人目前所在公司已经在使用没有发 ...
- jmeter设置为中文
我的jmeter安装路径是在D:\Jmeter\apache-jmeter-5.1.1\bin. 设置中文有2种方法: 1.第一种方法:点击jmeter.bat进入jmeter界面,点击[option ...
- CF235C-Cyclical Quest【SAM】
正题 题目链接:https://www.luogu.com.cn/problem/CF235C 题目大意 一个文本串\(s\).询问\(n\)个匹配的本质不同的循环同构在文本串中出现了几次. 解题思路 ...
- Markdown 编写技巧汇总(一)
编写文档,有很多格式选择,也有不同平台选择.下面就自己接触到的MarkDown编写文档的各种技巧做简单梳理,供自己参阅,也希望帮到网友. [1]添加空格 ① 这种写法比较老土,但是,很实用!注意都 ...
- WPF实现统计图(饼图仿LiveCharts)
WPF开发者QQ群: 340500857 | 微信群 -> 进入公众号主页 加入组织 每日一笑 下班和实习生一起回家,公交站等车,一乞丐把碗推向实习生乞讨.这时,实习生不慌不忙的说了句:&qu ...
- DBeaver MSSQL 支持TLS设置
DBeaver是一个基于 Java 开发,免费开源的通用数据库管理和开发工具,使用非常友好的 ASL 协议.可以通过官方网站或者 Github 进行下载. 由于 DBeaver 基于 Java 开发, ...
- Java运行时异常与非运行时异常
Java运行时异常与非运行时异常 Exception(异常)是程序本身可以处理的异常.主要包含RuntimeException等运行时异常和IOException,SQLException等非运行时异 ...