Farm Irrigation(非常有意思的并查集)
Farm Irrigation
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 46 Accepted Submission(s) : 26
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Figure 1
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
Figure 2
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?
Note: In the above example, at least 3 wellsprings are needed, as those red points in Figure 2 show.
Input
Output
Sample Input
2 2
DK
HF 3 3
ADC
FJK
IHE -1 -1
Sample Output
2
3
Author
Source
#include <iostream> using namespace std;
char map[][];
int par[];
int find(int x)
{
while(x!=par[x])
x=par[x];
return x;
}
void unioni(int x,int y)
{
int xx=find(x);
int yy=find(y);
if(xx!=yy)
{
par[yy]=xx;
} }
int main()
{
int n,m;
while(cin>>n>>m)
{
if(n<||m<)
break;
for(int i=;i<n*m;i++)
par[i]=i;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
cin>>map[i][j];
} for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(i>=&&(map[i][j]=='A'||map[i][j]=='B'||map[i][j]=='E'||map[i][j]=='G'||map[i][j]=='H'||map[i][j]=='J'||map[i][j]=='K'))
{
if(map[i-][j]=='C'||map[i-][j]=='D'||map[i-][j]=='E'||map[i-][j]=='H'||map[i-][j]=='I'||map[i-][j]=='J'||map[i-][j]=='K')
{
unioni((i-)*m+j,i*m+j);
}
}
if(j>=&&(map[i][j]=='A'||map[i][j]=='C'||map[i][j]=='F'||map[i][j]=='G'||map[i][j]=='H'||map[i][j]=='I'||map[i][j]=='K'))
if(map[i][j-]=='B'||map[i][j-]=='D'||map[i][j-]=='F'||map[i][j-]=='G'||map[i][j-]=='I'||map[i][j-]=='J'||map[i][j-]=='K')
{
unioni(i*m+j-,i*m+j);
}
}
int flag=;
for(int i=;i<n*m;i++)
{
if(par[i]==i)
flag++; }
cout<<flag<<endl; }
return ;
}
Farm Irrigation(非常有意思的并查集)的更多相关文章
- HDU1198水管并查集Farm Irrigation
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...
- 【简单并查集】Farm Irrigation
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- 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(深搜dfs || 并查集)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...
- hdu 1198 Farm Irrigation(并查集)
题意: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...
- 杭电OJ——1198 Farm Irrigation (并查集)
畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...
- hdu1198 Farm Irrigation 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 简单并查集 分别合并竖直方向和水平方向即可 代码: #include<iostream&g ...
- hdu 1198 (并查集 or dfs) Farm Irrigation
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...
- HDU 1198 Farm Irrigation(并查集+位运算)
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
随机推荐
- progressBar显示百分比
this.lab_AllFiles.Text = progressBarAllFile.Value * 100 / progressBarAllFile.Maximum + "%" ...
- JaveWeb 公司项目(7)----- 通过JS动态生成DIV
Web网页项目的数据表格功能已经大体完成,下面是另一个主要功能,在线视频的显示 目前我做的项目是渔政监控方面,在之前C#的版本中已经实现了摄像头的在线监控,用的海康封装好的SDK,目前需要做的工作是在 ...
- java递归 处理权限管理菜单树或分类
1.数据库表设计 2.实体类设计 package com.ieou.capsule.dto.SystemPermissions; import java.util.List; /** * 功能菜单类 ...
- python的json模块的dumps,loads,dump,load方法介绍
dumps和loads方法都在内存中转换, dump和load的方法会多一个步骤,dump是把序列化后的字符串写到一个文件中,而load是从一个文件中读取字符串 将列表转为字符串 >>&g ...
- leecode第九题(回文数)
class Solution { public: bool isPalindrome(int x) { ) return false; ;//这里使用long,也不判断溢出了,反正翻转不等就不是回文 ...
- => js 中箭头函数使用总结
箭头函数感性认识 箭头函数 是在es6 中添加的一种规范 x => x * x 相当于 function(x){return x*x} 箭头函数相当于 匿名函数, 简化了函数的定义. 语言的发展 ...
- DRF中的APIView、GenericAPIView、ViewSet
1.APIView(rest_framework.views import APIView),是REST framework提供的所有视图的基类,继承自Django的View. 传入到视图方法中的是R ...
- VNC安装配置
1,安装VNC yum install tigervnc-server -y2,设定密码: vncpasswd root 3,配置服务文件,下面是配置了6个窗口. cp /lib/systemd/sy ...
- Java面试宝典(说说&和&&的区别)
&和&&都可以用作逻辑与的运算符,表示逻辑与(and),当运算符两边的表达式的结果都为true时,整个运算结果才为true,否则,只要有一方为false,则结果为false. ...
- 第 6 章 存储 - 044 - volume 生命周期管理
volume 生命周期管理 1)备份 因为 volume 实际上是 host 文件系统中的目录和文件,所以 volume 的备份实际上是对文件系统的备份 例如:本地的Registry,所有镜像都存在/ ...