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种选择,然后相邻两个图只有都和对方连接,才判断他们 ...
随机推荐
- python中安装第三方库(使用豆瓣的镜像网站快速安装)
#安装第三方库#标准库,第三方库#pip install selenium 直接从官网进行安装,有时网速会有点慢#可以去国内的镜像站安装#pip install selenium -i https:/ ...
- Catch That Cow----BFS
Catch That Cow Description 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上 ,农夫起始位于点 N(0<=N<=100000) ,牛位于点 K(0<= ...
- Vue3的新特性及相关的Composition API使用
首先 创建项目 Vue3 Vue3 相较于Vue2 的6大亮点: 1 性能快. 2 按需编译 体积更小 3 提供了组合API 类似于react 的React Hooks 4 更好的Ts支持 5 暴露了 ...
- PHP的那些魔术方法(二)
上文中介绍了非常常用并且也是面试时的热门魔术方法,而这篇文章中的所介绍的或许并不是那么常用,但绝对是加分项.当你能准确地说出这些方法及作用的时候,相信对方更能对你刮目相看. __sleep()与__w ...
- MySql WorkBench通过表生成表关系图
1.mysql workbench 菜单file=>add model(添加模型) 点击上面的add diagram(添加新的图解),就会在右边多出一个新的图解模型 2,mysql workbe ...
- Chrome插件 - Modify Headers for Google Chrome(IP欺骗)
前景: 该篇随笔的由来:公司某项目(B/S架构)最近新加了一个后台日志功能,需要抓取到访问项目的主机IP,记录目标主机的操作,因此就需要不同得IP访问.并且项目专用浏览器是Chrome内核. Modi ...
- centos7安装sonarqube与使用
https://www.cnblogs.com/mascot1/p/11179767.html https://blog.csdn.net/superbfly/article/details/1039 ...
- 对帧率、I/P率、I帧间隔的理解
码率就是数据传输时单位时间传送的数据位数,一般我们用的单位是kbps即千位每秒.通俗一点的理解就是取样率,单位时间内取样率越大,精度就越高,处理出来的文件就越接近原始文件,但是文件体积与取样率是成正比 ...
- 屏幕截图小工具的制作过程问题记录 python PIL pynput pyautogui pyscreeze
最近想做一个脚本小工具,方便写一些操作说明文档,它的功能很简单,就是把脚本打开之后,鼠标进行操作点击时,会在点击后进行截图,并在图上标记出点击的位置,有点类似于录屏软件的图片版,这样的话,如果要想用文 ...
- dbus客户端使用指南
DBus是Linux使用的进程间通信机制,允许各个进程互相访问,而不需要为每个其他组件实现自定义代码.即使对于系统管理员来说,这也是一个相当深奥的主题,但它确实有助于解释linux的另一部分是如何工作 ...