【简单并查集】Farm Irrigation
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
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
Output
Sample Input
2 2
DK
HF 3 3
ADC
FJK
IHE -1 -1
Sample Output
2
3
Author
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的更多相关文章
- HDU1198水管并查集Farm Irrigation
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...
- POJ 2524 (简单并查集) Ubiquitous Religions
题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...
- poj1611 简单并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 32781 Accepted: 15902 De ...
- 1213 How Many Tables(简单并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...
- ACM_“打老虎”的背后(简单并查集)
“打老虎”的背后 Time Limit: 2000/1000ms (Java/Others) Problem Description: “习大大”自担任国家主席以来大力反腐倡廉,各地打击贪腐力度也逐步 ...
- poj1988 简单并查集
B - 叠叠乐 Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:30000KB 64bit ...
- UVA - 1197 (简单并查集计数)
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...
- poj 2524 Ubiquitous Religions 一简单并查集
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 22389 Accepted ...
- ACM_城市交通线(简单并查集)
城市交通线 Time Limit: 2000/1000ms (Java/Others) Problem Description: A国有n座城市,编号为1~n,这n个城市之间没有任何交通线路,所以不同 ...
随机推荐
- urllib2 源码小剖
urllib2 源码小剖 2013-08-25 23:38 by 捣乱小子, 272 阅读, 0 评论, 收藏, 编辑 两篇小剖已经完成: urllib 源码小剖 urllib2 源码小剖 urlli ...
- 转---高并发Web服务的演变——节约系统内存和CPU
[问底]徐汉彬:高并发Web服务的演变——节约系统内存和CPU 发表于22小时前| 4223次阅读| 来源CSDN| 22 条评论| 作者徐汉彬 问底Web服务内存CPU并发徐汉彬 摘要:现在的Web ...
- C#多线程解决界面卡死问题
C#多线程解决界面卡死问题的完美解决方案 文章下最方有源码下载 问题描述:当我们的界面需要在程序运行中不断更新数据时, 当一个textbox的数据需要变化时, 对于这个问题可以先参考下我的另外一个文章 ...
- Citrix 服务器虚拟化之一 网络部署Xenserver 6.2
Citrix 服务器虚拟化之一 网络部署Xenserver 6.2 思杰的XenServer®是完整的服务器虚拟化平台. XenServer软件包中包含所有你需要创建和管理部署的虚拟x86计算机上运 ...
- 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 ...
- Android开发(25)--framebyframe帧动画并实现启动界面到主界面的跳转
Drawable animation可以加载Drawable资源实现帧动画.AnimationDrawable是实现Drawable animations的基本类.推荐用XML文件的方法实现Drawa ...
- ok6410 u-boot-2012.04.01移植二修改源码支持单板
继ok6410 u-boot-2012.04.01移植一后修改代码,对ok6410单板初始化,主要包括时钟.串口.NAND.DDR等初始化.这些工作在以前的裸板程序都写了,直接拿来用.我觉得先写裸板程 ...
- message from server: "Host 'xxx' is not allowed to connect to this MySQL server的解决
解决方法: 1. 改表法. 可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" ...
- JS高程5.引用类型(5)Array类型的操作方法
一.操作方法 1.concat()方法 基于当前数组中的所有项创建一个新数组.具体说,是先创建当前数组的一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组.在没有给concat() ...
- 关于Ueditor 前后端分离实现文件上传到独立服务器的问题 望大神们赐教
最近,由于网站实现多台服务器负载均衡,导致编辑器上传文件需要同步,可是使用同步软件太慢,不太现实,所以想到实现编辑器上传文件直接上传到独立文件服务器.可是没想到遇到坑了. 1.在本地IIS 中添加网站 ...