hdu1198 Farm Irrigation(DFS,并查集)
来源:互联网 发布:风油精滴b 知乎 编辑:程序博客网 时间:2024/05/18 03:56
Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4742 Accepted Submission(s): 2056
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
听说是一道并查集的题
这里用DFS做
每个方块用3*3的bool型表示,1为有水管的地方,0表示没有
#include<stdio.h>#include<string.h>int m,n;char s[54][54];bool map[160][160],vis[160][160];int dir[4][2]= {0,1,1,0,-1,0,0,-1};void dfs(int x,int y){ if(x<0||x>=m*3||y<0||y>=n*3||!map[x][y]||vis[x][y]) return; vis[x][y]=1; for(int i=0; i<4; i++) dfs(x+dir[i][0],y+dir[i][1]);}int main(){ int i,j,ans; while(scanf("%d%d",&m,&n),m!=-1) { memset(map,0,sizeof(map)); memset(vis,0,sizeof(vis)); for(i=0; i<m; i++) { scanf("%s",s[i]); for(j=0; j<n; j++) { if(s[i][j]=='A') { map[i*3][j*3+1]=1; map[i*3+1][j*3]=1; map[i*3+1][j*3+1]=1; } else if(s[i][j]=='B') { map[i*3][j*3+1]=1; map[i*3+1][j*3+1]=1; map[i*3+1][j*3+2]=1; } else if(s[i][j]=='C') { map[i*3+1][j*3]=1; map[i*3+1][j*3+1]=1; map[i*3+2][j*3+1]=1; } else if(s[i][j]=='D') { map[i*3+1][j*3+1]=1; map[i*3+1][j*3+2]=1; map[i*3+2][j*3+1]=1; } else if(s[i][j]=='E') { map[i*3][j*3+1]=1; map[i*3+1][j*3+1]=1; map[i*3+2][j*3+1]=1; } else if(s[i][j]=='F') { map[i*3+1][j*3]=1; map[i*3+1][j*3+1]=1; map[i*3+1][j*3+2]=1; } else if(s[i][j]=='G') { map[i*3][j*3+1]=1; map[i*3+1][j*3]=1; map[i*3+1][j*3+1]=1; map[i*3+1][j*3+2]=1; } else if(s[i][j]=='H') { map[i*3][j*3+1]=1; map[i*3+1][j*3]=1; map[i*3+1][j*3+1]=1; map[i*3+2][j*3+1]=1; } else if(s[i][j]=='I') { map[i*3+1][j*3]=1; map[i*3+1][j*3+1]=1; map[i*3+1][j*3+2]=1; map[i*3+2][j*3+1]=1; } else if(s[i][j]=='J') { map[i*3][j*3+1]=1; map[i*3+1][j*3+1]=1; map[i*3+1][j*3+2]=1; map[i*3+2][j*3+1]=1; } else { map[i*3][j*3+1]=1; map[i*3+1][j*3]=1; map[i*3+1][j*3+1]=1; map[i*3+1][j*3+2]=1; map[i*3+2][j*3+1]=1; } } } ans=0; for(i=0; i<m*3; i++) { for(j=0; j<n*3; j++) { if(map[i][j]&&!vis[i][j]) { dfs(i,j); ans++; } } } printf("%d\n",ans); } return 0;}
0 0
- hdu1198 Farm Irrigation(DFS,并查集)
- HDU1198(Farm Irrigation)-并查集
- hdu1198 Farm Irrigation —— dfs or 并查集
- hdu1198 Farm Irrigation 并查集或者dfs
- HDU1198-Farm Irrigation(并查集应用)
- Hdu1198 - Farm Irrigation - 并查集
- hdu1198 Farm Irrigation ----并查集
- hdu1198 Farm Irrigation dfs
- hdu1198 Farm Irrigation DFS
- hdu1198 Farm Irrigation(DFS)
- hdu1198 Farm Irrigation (DFS)
- 【hdu1198】Farm Irrigation——并查集
- hdu 1198 Farm Irrigation(深搜dfs || 并查集)
- HDU 1198 Farm Irrigation (dfs / 并查集)
- 【并查集】Farm Irrigation
- Farm Irrigation(并查集)
- 【HDU 1198】Farm Irrigation(dfs+并查集+bfs)
- zoj2412 & hdu1198 - Farm Irrigation (DFS实现)
- linux fileno函数
- 如何把一个软件系统搞成saas
- sed
- 一种服务器的负载均衡选取算法
- popwindow 加listview OnItemClickListener失效问题
- hdu1198 Farm Irrigation(DFS,并查集)
- 想证明自己,三年已足够。
- Socket
- C++ General: How is floating point representated?
- 矢量图形的一些资料
- 面试题整理 4 合并两个排序的数组
- 驱蝗运动
- linux more命令参数及用法详解---逐页显示文件内容方便阅读
- 怎么让vi编辑时显示颜色