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. Java快速开发工具 WebBuilder 6.8发布

    WebBuilder是一款开源的可视化Java Web应用开发和运行平台: 基于浏览器的集成开发环境,可视化和智能化的设计,能轻松完成常规应用和面向手机的移动应用开发: 高效.稳定和可扩展的特点,适合 ...

  2. jquery设置元素的readonly和disabled【转】

    Jquery的api中提供了对元素应用disabled和readonly属性的方法,在这里记录下.如下: 1.readonly    $('input').attr("readonly&qu ...

  3. C# 对象池的实现

    C# 对象池的实现 对象池服务可以减少从头创建每个对象的系统开销.在激活对象时,它从池中提取.在停用对象时,它放回池中,等待下一个请求.我们来看下主线程中,如何与对象池打交道: static void ...

  4. 微软IE11浏览器的7大变化

    微软IE11浏览器的7大变化 投递人 itwriter 发布于 2013-06-29 11:48 评论(9) 有1025人阅读  原文链接  [收藏]  « » 微软很看重自己的 IE 浏览器,这款浏 ...

  5. Wcf 文件上传下载

    wcf 文件上传的例子网上很多,我也是借鉴别人的示例.wcf 文件下载的示例网上就很少了,不知道是不是因为两者的处理方式比较类似,别人就没有再上传了.在此本人做下记录备忘. UploadFile.sv ...

  6. Left 、right join使用笔记

    用过很多次left join,right join,看到查询出的结果,知道可能是没用left join.或者right join导致的结果,但都是用一次,网上查一次使用规则,学到的东西只是皮毛.今天用 ...

  7. Android 获得各处图片的方法

    <pre name="code" class="java">//1,已将图片保存到drawable目录下 //通过图片id获得Drawable Re ...

  8. Windows 8.1 Preview的新功能和新API

    http://msdn.microsoft.com/en-us/library/windows/apps/bg182410 App打包 新的App程序包将使App的提交更简单.资源包可以让你提供附加的 ...

  9. ON COMMIT PRESERVE ROWS

    定义声明式全局临时表的每个会话拥有自己的独特的临时表描述.当会话终止时,表行和临时表描述均会被删除. 有如下选项,可控制commit后临时表的状态: ON COMMIT DELETE ROWS:在执行 ...

  10. js区分汉字和字符,校验长度

    遇到这么一个问题,    长度限制输入150个英文字符(小于等于150个英文字符长度),超出则直接禁止输入,并提醒:摘要输入必须小于等于75个中文字符长度! 长度校验倒是没问题,但是要区分汉字还是英文 ...