Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4479    Accepted Submission(s): 1942

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
 #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(并查集)的更多相关文章

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

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

  2. hdu1198 Farm Irrigation 并查集

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

  3. HDU1198水管并查集Farm Irrigation

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

  4. 【简单并查集】Farm Irrigation

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

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

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

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

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

  7. hdu 1198 Farm Irrigation(并查集)

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

  8. Farm Irrigation(非常有意思的并查集)

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

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

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

随机推荐

  1. javascript 高级程序设计(三)-数据类型

    ECMAScript 中所有类型的值都有与这两个Boolean值等价的值   数据类型     转换为true的值   转换为false的值 Boolean true   false( String ...

  2. React组件的生命周期各环节运作流程

    'use strict'; React.createClass({ //1.创建阶段 getDefaultProps:function(){ //在创建类的时候被调用 console.log('get ...

  3. jquery实现很简单的DIV拖动

    今天用jquery实现了一个很简单的拖动...实现思路很简单  如下: 在thickbox 弹出层内实现拖拽DIV,那么得进行一下相对宽高的运算:必须加上相对于可见窗口宽高和弹出层宽高之间的差:    ...

  4. Const和ReadOnly

    总结一下const和readonly有这么几条区别: const和readonly的值一旦初始化则都不再可以改写: const只能在声明时初始化:readonly既可以在声明时初始化也可以在构造器中初 ...

  5. 【读书笔记】管道和FIFO

    管道 提供一个单路(单向)数据流,可以为两个不同进程提供进程间的通信手段 #include <unistd.h> ]); 返回两个文件描述符,fd[0](读) 和 fd[1](写) 管道间 ...

  6. oracle中不曾熟悉的 to_char、to_number(未完待续)

    十进制       十六进制88               58 用法一:Converts a HEX number  to  o FLOAT (转换一个十六进制数的浮标) SQL> sele ...

  7. IOS学习--UIButton常用方法(20150122)

    // 1.创建一个自定义的按钮 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; // 2.添加按钮 [self.view a ...

  8. Java线程(学习整理)--1--守护线程

    1.什么是守护线程? 今天老师讲解我才知道有守护线程这回事!原来守护线程经常存在于我们的身边,比如:一个免费的网页游戏,里面都会或多或少有些插入性的广告!! 一般情况下,我们不会去点击这些广告的,但是 ...

  9. Spring 创建bean的时机

    默认在启动spring容器的时候,spring容器配置文件中的类就已经创建完成对象了        在<bean>中添加属性lazy-init,默认值为false.    true  在c ...

  10. hdoj 1686 kmp

    题目:   Sample Input 3 BAPC BAPC AZA AZAZAZA VERDI AVERDXIVYERDIAN   Sample Output 1 3 0     代码:   #in ...