Farm Irrigation

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 6   Accepted Submission(s) : 3

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 1Benny 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 2Several 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

题解:使用并查集,遍历每个格子的方向,把可以连通的相邻的两个格子合并成一个集合。输出N*M-Count就是独立集合的个数、

注意,并查集的数组需要开大点,开小了结果Tle了好几次。。。

代码:2015/10/20

 #include <iostream>
#include <stdio.h>
#define Max 5200
using namespace std;
int DicX[]={,-,,};
int DicY[]={-,,,};
int Sign[][]={
,,,,//A
,,,,//B
,,,,//C
,,,,//D
,,,,//E
,,,,//F
,,,,//G
,,,,//H
,,,,//I
,,,,//J
,,,//K
};
int ID[Max];
char Str[Max][Max];
void Cread(int N)
{
for(int i=;i<=N;i++)ID[i]=i;
}
int Find(int x)
{
if(x!=ID[x])ID[x]=Find(ID[x]);
return ID[x];
}
int main()
{
int N,M,i,j,k,d;
while(scanf("%d%d",&N,&M)!=EOF)
{
if(N<||M<)break;
for(i=;i<N;i++){
scanf("%s",Str[i]);
}
int Count=;
Cread(N*M);
for(i=;i<N;i++)//遍历每一个格子
{
for(j=;j<M;j++)
{
for(k=;k<;k++)//遍历当前格子的四个方向
{
int ii=i+DicX[k];
int jj=j+DicY[k]; if(ii<||jj<||ii>=N||jj>=M)continue;
if(Sign[Str[i][j]-'A'][k]&&Sign[Str[ii][jj]-'A'][(k+)%])
{//判断当前格子的四个方向与所相邻格子的是否可连通
int A=Find(i*M+j);
int B=Find(ii*M+jj);
if(A!=B)//合并可连通的集合
{
ID[A]=B;
Count++;
}
}
}
}
}
printf("%d\n",N*M-Count);//输出集合的个数
}
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. POJ 2524 (简单并查集) Ubiquitous Religions

    题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...

  3. poj1611 简单并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 32781   Accepted: 15902 De ...

  4. 1213 How Many Tables(简单并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...

  5. ACM_“打老虎”的背后(简单并查集)

    “打老虎”的背后 Time Limit: 2000/1000ms (Java/Others) Problem Description: “习大大”自担任国家主席以来大力反腐倡廉,各地打击贪腐力度也逐步 ...

  6. poj1988 简单并查集

    B - 叠叠乐 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:30000KB     64bit ...

  7. UVA - 1197 (简单并查集计数)

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

  8. poj 2524 Ubiquitous Religions 一简单并查集

    Ubiquitous Religions   Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 22389   Accepted ...

  9. ACM_城市交通线(简单并查集)

    城市交通线 Time Limit: 2000/1000ms (Java/Others) Problem Description: A国有n座城市,编号为1~n,这n个城市之间没有任何交通线路,所以不同 ...

随机推荐

  1. C++11改进我们的程序之简化我们的程序1

    C++11改进我们的程序之简化我们的程序(一) C++11在很多方面可以简化我们的程序开发,我会在“简化我们的程序”这一系列的博文中一一讲到,敬请关注.这次要讲的是:C++11如何通过获取函数模板的返 ...

  2. javascript生成自定义的arcgis simpletoolbar

    javascript生成自定义的arcgis simpletoolbar 最近在学习ARCGIS for Javascript过程中,在ESRI的在线帮助上看见了这样一个示例,查看源码后,觉得左侧工具 ...

  3. ubunt 命令行下链接VPN

    Ubuntu命令行下VPN连接与使用 route add default dev ppp0 route add -net 192.168.2.0 netmask 255.255.255.0 ppp0 ...

  4. kivy Grid Layout

    http://kivy.org/docs/api-kivy.uix.gridlayout.html?highlight=gridlayout#kivy.uix.gridlayout It's so n ...

  5. [置顶] SQL注入安全分析

    (一)       应用环境列表 网络互联设备操作系统 序号 操作系统名称 设备名称 脆弱性 1 IOS_路由器_内部_1 route1 2 IOS_路由器_VPN_1 路由器_VPN_1 3 IOS ...

  6. jsp传值乱码解决办法

    在jsp中,我们经常从数据库读取数据返回客户端,但我们常常在制作时出现乱码现象,所以我们可以用<%request.setCharacterEncoding("UTF-8"); ...

  7. Java自带的性能监测工具用法简介——jstack、jconsole、jinfo、jmap、jdb、jsta、jvisualvm

    JDK内置工具使用 一.javah命令(C Header and Stub File Generator) 二.jps命令(Java Virtual Machine Process Status To ...

  8. 如何把微信语音汇总成一个MP3文件?

    有的时候想要保存微信中的语音内容,但是苦于语音短且多,因此想要把它汇总成一个音频文件. 本篇以苹果手机为例,安卓手机也可类似. 第一步,安装同步助手 同步助手是一款在电脑上安装,可以保存手机上的内容的 ...

  9. JavaScript进阶(四)

    现在说说什么是函数.函数的作用可以写一次代码,然后反复的重用这个代码.如:我们要完成多组数和的功能.var sum;sum=3+2;alert(sum); sum=7+8;alert(sum);... ...

  10. 试制品 (nyoj 542)

    模拟 a 反应物集合 ; b 生成物集合; c 存在的化合物或单质集合; ans 新生成化合物集合 1.如果反应无均在已生成的化合物集合中,则完成反应,将合成物加入c集合 2.对每个方程式的反应物进行 ...