Farm Irrigation(并查集)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4479 Accepted Submission(s): 1942
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.
#include<stdio.h>
#include<string.h>
const int N = ;
int n,m;
char map[N][N];
int set[N*N];
int pipe[][] = {{,,,},{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,}
};//十一种水管,每四个数字描述一种管子,表示它的上右下左
//是否有管口; void init()
{
for(int i = ; i < n*m; i++)
set[i] = i;
} int find(int x)
{
if(set[x] != x)
set[x] = find(set[x]);
return set[x];
} int judge(char a, char b, int pos)
{
int x = a-'A';
int y = b-'A'; if(pos)
{
if(pipe[x][] == && pipe[y][] == )
return ;
else return ;
}
else
{
if(pipe[x][] == && pipe[y][] == )
return ;
else return ;
}
} void merge(int a,int b)
{
a = find(a);
b = find(b);
set[a] = b;
} int main()
{
int i,j;
while(~scanf("%d %d",&n,&m))
{
if(n == - && m == -)
break;
getchar();
init();
for(i = ; i < n; i++)
gets(map[i]); for(i = ; i < n; i++)
{
for(j = ; j < m; j++)
{
if(i != n-)
{
if(judge(map[i][j],map[i+][j],))//1表示两个字母所代表的管子位置关系是上下;
{
merge(i*m+j,(i+)*m+j);
}
}
if(j != m-)
{
if(judge(map[i][j],map[i][j+],))//0表示两个字母所代表的管子位置关系是左右;
{
merge(i*m+j,i*m+j+);
}
}
}
}
int count = ;
for(i = ; i < n*m; i++)
{
if(set[i] == i)
count++;
}
printf("%d\n",count); }
return ;
}
Farm Irrigation(并查集)的更多相关文章
- 杭电OJ——1198 Farm Irrigation (并查集)
畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...
- hdu1198 Farm Irrigation 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 简单并查集 分别合并竖直方向和水平方向即可 代码: #include<iostream&g ...
- 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 ...
- Farm Irrigation(非常有意思的并查集)
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- hdu 1198 (并查集 or dfs) Farm Irrigation
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...
随机推荐
- 【Android】数据的应用-使用sharedpreferences存储数据
Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...
- HTML的Get方法URL传递中文参数,解决乱码问题
本例中有使用JQuery. 资料参考:http://www.cnblogs.com/babycool/p/3169058.html 发送的HTML页面代码: <!DOCTYPE html> ...
- http学习笔记一
- PHP替换数据库的换行符
//php 有三种方法来解决 //1.使用str_replace 来替换换行 $str = str_replace(array("\r\n", "\r", &q ...
- Html网页生成Pdf
在http://code.google.com/p/wkhtmltopdf/downloads/list下载安装程序. 1.添加引用 using System.Diagnostics; 添加引用 2. ...
- Notification和KVO有什么不同
Notification是推送通知,我们可以建立一个通知中心,存放创建多个通知,在不同的地方在需要的时候push调用和KVO不同的是,KVO是键值观察,只能观察一个值,这就是区别
- C#读取Visual FoxPro(*.dbf)数据并使用SqlBulkCopy插入到SqlServer 2008 R2数据表中
公司数据库从32位的SqlServer 2005升级到64位的SqlServer 2008 R2后,无法再像原来通过Link Server连接VFP同步数据,因此考虑用代码程序从VFP数据库中读取所需 ...
- 颜色rgb
1.几种基本颜色的rgb 黑色:R.G.B(0.0.0) 白色:R.G.B(255.255.255) 红色:R.G.B(255.0.0) 绿色:R.G.B(0.255.0) 蓝色:R.G.B(0.0. ...
- ktv
自制KTV点歌系统经验 Windows Media Player控件播放 Windows Media Player控件的简单使用 1.播放一首歌曲的方法 Windows Media Pla ...
- python 数据结构
Python的数据结构主要分为set(),list[],和dict{}.这篇文章主要记载这几种结果在使用过程中的一些技巧或其中一些函数的用法区别. 1.函数get()与setdefault()区别: ...