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. web.xml加载过程

    web.xml加载过程:1 启动WEB项目的时候,容器(如:Tomcat)会读他的配置文件web.xml读两个节点  <listener></listener>和<con ...

  2. (Sql Server)数据的拆分和合并

    (Sql Server)数据的拆分和合并 背景: 今天遇到了数据合并和拆分的问题,尝试了几种写法.但大致可分为两类:一.原始写法.二.Sql Server 2005之后支持的写法.第一种写法复杂而且效 ...

  3. iOS 7 beta4 体验

    iOS 7 beta4终于来了,安装后感觉稳定了不少.下面列几点我个人感受比较深得地方. 1.锁屏界面有滑动方向箭头了,而且“滑动来解锁”几个字也有动态颜色变化,让人不再迷惑该往那边滑动了. 2.通知 ...

  4. 10277 - Boastin' Red Socks

    描述:红黑袜子,给出的是红袜子被选到的概率,即为p/q,要计算的是在挑选出一对红袜子之前的红袜子和黑袜子的数目,假设红袜子数为n,黑袜子数为m,那么n(n-1)/(m(m-1))=p/q,求出红袜子数 ...

  5. c# datagridview 设置某行不可见解决办法

    [前提]datagridview与数据库绑定,需要单独设置某行或者某个单元格不可见. [问题分析]直接用this.dataGridCiew1.Rows[0].Visible = false;不可行,会 ...

  6. 了解OData(一)

    了解OData(一) 最近做了一个小项目,其中用到了 WCF Data Service,之前是叫 ADO.NET Data Service 的.关于WCF Data Service,博客园里的介绍并不 ...

  7. VIM批量文件查找和替换

    使用vim时间不长,linux命令行下常用的文本编辑工具,所以需要掌握一些基本的用法.很多不会的不是百度就谷歌,总有你想要的答案. 1. 批量文件查找内容 vimgrep 比如在当前目录下查找带有“a ...

  8. cocos2d-x场景切换与过渡效果

    场景切换 void  MyScene::daySceneCallback(CCObject *pSender) {     CCScene *scene =  new  MyScene();      ...

  9. poj1483 It's not a Bug, It's a Feature!

    It's not a Bug, It's a Feature! Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 1231   ...

  10. [置顶] logistic回归(一)

    先介绍下基础的公式: 这个是Sigmoid函数,在这个回归过程中非常重要的函数,主要的算法思想和这个密切相关.这个函数的性质大家可以自己下去分析,这里就不细说了. 然后我们说明下流程,首先我们将每个特 ...