首页 > 代码库 > HDU 1198-Farm Irrigation(并查集)
HDU 1198-Farm Irrigation(并查集)
Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5820 Accepted Submission(s): 2523
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 2 DK HF 3 3 ADC FJK IHE -1 -1
Sample Output
2 3很坑爹的并查集,输入以字母给出,所以对于每一个字母,都要判断它是否和上下左右的字母连通,如果连通,则将它们合并,最后集合的个数就是答案。很无奈我只好在纸上把和所有相连通的字母找出来然后存在了数组里面。#include <iostream> #include <cstdio> #include <cstring> using namespace std; int m,n,fa[10010],hash[10010]; char ma[60][60]; void Make_set(int n) { for(int i=1;i<=n;i++) fa[i]=i; } int Find(int x) { if(x!=fa[x]) fa[x]=Find(fa[x]); return fa[x]; } void Union(int x,int y) { int fx=Find(x); int fy=Find(y); if(fx==fy) return ; fa[fy]=fx; } int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; char ok[11][4][12]={{"CDEHIJK","","BDFGIJK",""},{"CDEHIJK","","","ACFGHIK"},{"","ABEGHJK","BDFGIJK",""},{"","ABEGHJK","","ACFGHIK"},{"CDEHIJK","ABEGHJK","",""},{"","","BDFGIJK","ACFGHIK"},{"CDEHIJK","","BDFGIJK","ACFGHIK"},{"CDEHIJK","ABEGHJK","BDFGIJK",""},{"","ABEGHJK","BDFGIJK","ACFGHIK"},{"CDEHIJK","ABEGHJK","","ACFGHIK"},{"CDEHIJK","ABEGHJK","BDFGIJK","ACFGHIK"}}; void bfs(int x,int y) { int i,tx=x,ty=y; for(i=0;i<4;i++) { tx=x+dir[i][0]; ty=y+dir[i][1]; if(0<=tx&&tx<m&&0<=ty&&ty<n) { int flag=0,len=strlen(ok[ma[x][y]-'A'][i]); for(int j=0;j<len;j++) { if(ok[ma[x][y]-'A'][i][j]==ma[tx][ty]) { flag=1; break; } } if(flag) Union(x*n+y+1,tx*n+ty+1); } } } int main() { int i,j; while(~scanf("%d%d",&m,&n)) { memset(hash,0,sizeof(hash)); if(m==-1&&n==-1) break; for(i=0;i<m;i++) scanf("%s",ma[i]); Make_set(m*n); for(i=0;i<m;i++) for(j=0;j<n;j++) bfs(i,j); for(i=1;i<=m*n;i++) { int f=Find(i); hash[f]++; } int cnt=0; for(i=1;i<=m*n;i++) if(hash[i]) cnt++; printf("%d\n",cnt); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。