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

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.


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

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.

Output

For each test case, output in one line the least number of wellsprings needed.

Sample Input

2 2
DK
HF 3 3
ADC
FJK
IHE -1 -1

Sample Output

2
3

Author

ZHENG, Lu

Source

Zhejiang University Local Contest 2005
看这这几张图有点懵,其实只要仔细看,也没有很难。因为他的连通就是一个十字形。分成I和一考虑
与上面有联系的是 ABEGHJK(及下面出头)
与下面有联系的是CDEHIJK
 
 
与左边有联系的是 ACFGHIK
与右边有联系的是BDFGIJK
#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(非常有意思的并查集)的更多相关文章

  1. HDU1198水管并查集Farm Irrigation

    Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...

  2. 【简单并查集】Farm Irrigation

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  3. HDU 1198 Farm Irrigation(并查集,自己构造连通条件或者dfs)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. hdu 1198 Farm Irrigation(深搜dfs || 并查集)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...

  5. hdu 1198 Farm Irrigation(并查集)

    题意: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...

  6. 杭电OJ——1198 Farm Irrigation (并查集)

    畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...

  7. hdu1198 Farm Irrigation 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 简单并查集 分别合并竖直方向和水平方向即可 代码: #include<iostream&g ...

  8. hdu 1198 (并查集 or dfs) Farm Irrigation

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...

  9. HDU 1198 Farm Irrigation(并查集+位运算)

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

随机推荐

  1. JMeter中关于动态切换不同CSV文件解决方案

    最近写case,需要当前播放节目的数据作为输入数据,所以每个时刻所用的数据只能是当前时刻附件的数据,尝试用CSV Data Set Config动态加载不同的文件,没有成功,好像CSV Data Se ...

  2. 【BZOJ】3295: [Cqoi2011]动态逆序对

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3295 mamaya,弱鸡xrdog终于会写树套树啦.... 将树状数组中每一个节点看成一棵 ...

  3. uoj #228. 基础数据结构练习题 线段树

    #228. 基础数据结构练习题 统计 描述 提交 自定义测试 sylvia 是一个热爱学习的女孩子,今天她想要学习数据结构技巧. 在看了一些博客学了一些姿势后,她想要找一些数据结构题来练练手.于是她的 ...

  4. Java 中常见的各种排序算法汇总

    首先,Java中自已是有排序的 说明:(1)Arrays类中的sort()使用的是“经过调优的快速排序法”;(2)比如int[],double[],char[]等基数据类型的数组,Arrays类之只是 ...

  5. vue-cli3快速创建项目

    文档:https://cli.vuejs.org/zh/guide/ 条件: npm 更至最新 node >=8.9 1.全局安装 npm install -g @vue/cli 或 yarn ...

  6. RNN知识点

    1. RNN类型 2. RNN模型: 3.RNN和LSTM,由于LSTM 由于LSTM中,求Ct(记忆)是相加的形式,就可以保持长时间记忆,不像 4.LSTM中的控制参数C,Ct-1:前一时刻的记忆, ...

  7. 通过IP地址定位准确的地理位置

    事情的经过时这样的: 朋友发来一封QQ邮件原文,询问里面显示的IP地址是不是真是的IP地址.然后,我就解锁了一项新技能:通过IP地址定位准确的地理位置 在这里收藏一下这个网址:http://www.8 ...

  8. Inception网络

    2018-12-09 19:39:38 一.1 * 1卷积 pooling可以对feature map的height,width进行修改,但是对通道数目无法修改. 1 * 1卷积可以在不改变图像大小的 ...

  9. python中的静态方法、类方法、属性方法(福利:关于几种方法更好的解释)

    该部分的三个属性都是高级方法,平时用的地方不是很多 一.静态方法 静态方法的使用不是很多,可以理解的就看一下,用的地方不是很多 class Dog(object): def __init__(self ...

  10. BroadcastReceiver(广播)的静态注册和动态注册 --Android开发

    BroadcastReceiver是安卓四大组件之一,本例通过代码的方式演示静态注册和动态注册. 1.静态注册 静态注册只需要AndroidManifest.xml中进行配置: AndroidMani ...