Farm Irrigation

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

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<iostream>
#include<cstdio>
#include<cstring>
#define N 55
using namespace std; int n,m,f[N*N];
int map[][];
int dir[][]={{,-},{-,},{,},{,}}; int farm[][]={
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,}
}; void init(){ for(int i=; i<N*N; ++i)f[i]=i;} int findset(int x){ return f[x]!=x?f[x]=findset(f[x]):f[x];} void merge(int x,int y)
{
int a=findset(x), b=findset(y);
if(a==b)return ;
if(a<b) f[a]=b;
else f[b]=a;
} int main()
{
int i,j,k,dx,dy;
char ch;
while(scanf("%d%d%",&n,&m))
{
if(n==- && m==-) break;
for(i=; i<n; ++i)
{
for(j=; j<m; ++j)
{
scanf("%c",&ch);
map[i][j]=ch-'A';
}
getchar();
}
init();
for(i=; i<n; ++i)
{
for(j=; j<m; ++j)
{
for(k=; k<; ++k)
{
dx=i+dir[k][],dy=j+dir[k][];
if(dx<||dx>=n||dy<||dy>=m)continue;
if(k==)// 左
{
if(farm[map[dx][dy]][]&&farm[map[i][j]][]){
merge(dx*m+dy, i*m+j);
}
}
else if(k==)// 上
{
if(farm[map[dx][dy]][]&&farm[map[i][j]][]){
merge(dx*m+dy, i*m+j);
}
}
else if(k==)// 右
{
if(farm[map[dx][dy]][]&&farm[map[i][j]][]){
merge(dx*m+dy, i*m+j);
}
}
else if(k==)// 下
{
if(farm[map[dx][dy]][]&&farm[map[i][j]][]){
merge(dx*m+dy, i*m+j);
}
}
}
}
}
int cnt=;
for(i=; i<n*m; ++i)
if(f[i]==i)
++cnt;
printf("%d\n", cnt);
}
return ;
}
 

hdu 1189 并查集的更多相关文章

  1. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  2. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  3. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  4. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  5. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

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

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

  7. hdu 1598 (并查集加贪心) 速度与激情

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...

  8. hdu 4496(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...

  9. 2015多校第6场 HDU 5361 并查集,最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...

随机推荐

  1. PCL点云处理可视化——法向显示错误“no override found for vtk actor”解决方法

    转:https://blog.csdn.net/bflong/article/details/79137692 参照:https://blog.csdn.net/imsaws/article/deta ...

  2. OpenGL 渲染上下文-context

    context理解 OpenGL在渲染的时候需要一个Context,这个Context记录了OpenGL渲染需要的所有信息,可以把它理解成一个大的结构体,它里面记录了当前绘制使用的颜色.是否有光照计算 ...

  3. iOS开发资源:推送通知相关开源项目--PushSharp、APNS-PHP以及Pyapns等

    PushSharp  (github) PushSharp是一个实现了由服务器端向移动客户端推送消息的开源C#库,支持 iOS (iPhone/iPad APNS). Android (C2DM/GC ...

  4. 动态规划初步-单向STP

    一.题目 给一个m行n列(m <= 10,n <= 100)的整数矩阵,从第一列任何位置出发每次往右.右下.右上走一格,最终达到最后一列.要求经过的整数之和最小.整个矩阵是环形的,即第一行 ...

  5. iphone在jsp显示时间会NAN解决办法

    例:2018-12-28 15:00:00 1.   var  newDate = new Date("2018-12-28 15:00:00") 这种获取的时间在安卓手机上显示是 ...

  6. currentStyle和getComputedStyle来获取外部样式

    currentStyle和getComputedStyle来获取外部样式 通过document.getElementById(id).style.XXX就可以获取到XXX的值,但意外的是,这样做只能取 ...

  7. Docker基础内容之网络基础

    网络命名空间基本原理 单机版多容器实例网络交互原理 在宿主机上面打开两张网卡eth0与eth1,打通两张网卡的链路 在test1上面启动一个veth网卡,创建一个namespace:并桥接到eth0上 ...

  8. solr 单机模式搭建

    系统环境:centos 7 安装前准备 安装JDK环境 下载tomcat.solr安装包:solr下载地址:http://archive.apache.org/dist/lucene/solr/ 安装 ...

  9. 前端上传控件plupload总结

    plupload是一个单图和多图上传控件: 属性和方法介绍,参考以下博客: https://www.cnblogs.com/2050/p/3913184.html 这里直接贴出JS代码,细到爆的注释, ...

  10. css实现盒尺寸重置、均匀分布的子元素、截断文本

    盒尺寸重置 重置盒子模型,以便width s和height s并没有受到border 还是padding他们的影响 . CSS文字折断 css实现盒尺寸重置.均匀分布的子元素.截断文本 如何对多行文本 ...