hdu 1198 Farm Irrigation(并查集)
来源:互联网 发布:西南交大网络教学学费 编辑:程序博客网 时间:2024/05/20 22:39
Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4859 Accepted Submission(s): 2095
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.
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
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 2DKHF3 3ADCFJKIHE-1 -1
Sample Output
23
Author
ZHENG, Lu
Source
Zhejiang University Local Contest 2005
先用数组将11种水管罗列出来,再判断每块田的水管是否和其他相连,运用并查集,注意每块田只需判断它上面的田和左边的田即可,水井ans初始化为N*M,发现有水管相连且处于不同树的田则ans--;
详见代码:
#include<stdio.h>#include<string.h>int well[55][55][4];//记录每块田的4个方向是否有水管int root[3000];//[3000]将行列田转化为一维的,方便找根int Q[11][4]={ {1,0,1,0},{1,0,0,1},{0,1,1,0},{0,1,0,1},{1,1,0,0},{0,0,1,1},{1,0,1,1},{1,1,1,0},{0,1,1,1},{1,1,0,1},{1,1,1,1}};//11种水管[4]按顺序表示上,下,左,右int find_root(int n)//查{ if(n!=root[n]) root[n]=find_root(root[n]); return root[n];}void Union(int a,int b,int &ans)//并,注意传的是引用{ int ra,rb; ra=find_root(a); rb=find_root(b); if(ra!=rb)//不同的树合并 { root[rb]=ra; ans--; }}int main(){ int N,M; while(scanf("%d%d",&N,&M)!=EOF) { if(N<0||M<0)break; memset(well,0,sizeof(well)); int i,j; char w[55];; for(i=0;i<N;i++) { scanf("%s",w); for(j=0;j<M;j++) { int k; for(k=0;k<4;k++) well[i][j][k]=Q[w[j]-'A'][k]; } } int ans=N*M; for(i=0;i<N*M;i++) root[i]=i; for(i=0;i<N;i++) for(j=0;j<M;j++) { if(i!=0) { if(well[i][j][0]&&well[i-1][j][1]) Union((i-1)*M+j,i*M+j,ans); } if(j!=0) { if(well[i][j][2]&&well[i][j-1][3]) Union(i*M+j-1,i*M+j,ans); } } printf("%d\n",ans); } return 0;}
0 0
- hdu 1198 Farm Irrigation(并查集)
- hdu 1198 Farm Irrigation( 并查集)
- HDU 1198 Farm Irrigation(并查集)
- HDU 1198 Farm Irrigation 【并查集】
- HDU 1198Farm Irrigation(并查集)
- HDU 1198 Farm Irrigation(并查集)
- hdu 1198 Farm Irrigation(并查集)
- hdu 1198 Farm Irrigation(并查集)
- HDU 1198 Farm Irrigation 并查集
- HDU 1198 Farm Irrigation 并查集
- hdu-1198-Farm Irrigation(并查集)
- HDU 1198 Farm Irrigation (并查集)
- HDU 1198 Farm Irrigation 并查集
- hdu 1198 Farm Irrigation 并查集
- hdu 1198 Farm Irrigation(搜索+并查集)
- hdu 1198 Farm Irrigation (并查集)
- hdu 1198 Farm Irrigation(并查集)
- HDU 1198 Farm Irrigation(并查集)
- js返回页面顶部的方法
- 关于.INC文件
- SACU 判断一个数能否分解为几个连续自然数之和
- 关于朗格朗日函数法的一些知识
- ArcGIS 10.1 让GP服务变得更简单
- hdu 1198 Farm Irrigation(并查集)
- nodejs之express 从零开始
- unity3d]unity中几个系统自带的函数的执行顺序
- Java中StringBuffer类append方法的使用
- Android必备java知识点
- Memcached源码分析之Hash表操作
- 给定三角形的三边长,求三角形的面积
- 再谈我是怎么招聘程序员的
- Quartz定时任务在Spring MVC 中的实现