hdu1198 Farm Irrigation (DFS)
来源:互联网 发布:郑元畅为什么不红 知乎 编辑:程序博客网 时间:2024/05/17 23:43
Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8891 Accepted Submission(s): 3850
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
#include <iostream>#include<stdio.h>#include<string.h>#define ma 100using namespace std;int a[ma][ma],n,m;int ps[17]={1,1,0,0,1,0,1,1,0,1,1}, py[17]={0,1,0,1,0,1,1,0,1,1,1}, px[17]={0,0,1,1,1,0,0,1,1,1,1}, pz[17]={1,0,1,0,0,1,1,1,1,0,1}, h[8]={-1,-1,-1,0,0,1,1,1}, l[8]={-1,0,1,-1,1,-1,0,1};int flag[ma][ma]; int check(int h,int l,int x,int y) { if(h==-1&&l==0&&px[a[x+h][y+l]]==1&&ps[a[x][y]]==1)return 1; if(h==0&&l==-1&&py[a[x+h][y+l]]==1&&pz[a[x][y]]==1)return 1; if(h==0&&l==1&&pz[a[x+h][y+l]]==1&&py[a[x][y]]==1)return 1; if(h==1&&l==0&&ps[a[x+h][y+l]]==1&&px[a[x][y]]==1)return 1; return 0; }void dfs(int x,int y){ for(int k=0;k<8;k++) { if(x+h[k]<n&&x+h[k]>=0&& y+l[k]<m&&y+l[k]>=0&&flag[x+h[k]][y+l[k]]==0&& check(h[k],l[k],x,y)==1) { flag[x+h[k]][y+l[k]]=1; dfs(x+h[k],y+l[k]); } }}int main(){ while(1) { scanf("%d%d",&n,&m); if(n<0||m<0)break; char c[70]; for(int i=0;i<n;i++) { scanf("%s",&c); for(int j=0;j<m;j++) a[i][j]=c[j]-'A'; } memset(flag,0,sizeof(flag)); int ans=0; for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(flag[i][j]==0) { ans++; flag[i][j]=1; dfs(i,j); } printf("%d\n",ans); } return 0;}
0 0
- hdu1198 Farm Irrigation dfs
- hdu1198 Farm Irrigation DFS
- hdu1198 Farm Irrigation(DFS)
- hdu1198 Farm Irrigation (DFS)
- zoj2412 & hdu1198 - Farm Irrigation (DFS实现)
- HDU1198 Farm Irrigation
- HDU1198--Farm Irrigation
- HDU1198 Farm Irrigation
- Farm Irrigation(HDU1198)(幷查集)
- HDU1198 Farm Irrigation
- HDU1198-Farm Irrigation
- hdu1198 Farm Irrigation
- hdu1198 Farm Irrigation(DFS,并查集)
- hdu1198 Farm Irrigation —— dfs or 并查集
- hdu1198 Farm Irrigation 并查集或者dfs
- HDU1198(Farm Irrigation)-并查集
- HDU1198-Farm Irrigation(并查集应用)
- Hdu1198 - Farm Irrigation - 并查集
- LeetCode Unique Binary Search Trees 2 递归方法
- geekband android #5 第一周分享(初识Android)
- 无法执行添加/移除操作,因为代码元素**是只读的
- oracle的分析函数over及开窗关键字range
- 【SSH进阶之路】Spring的IOC逐层深入——依赖注入的两种实现类型
- hdu1198 Farm Irrigation (DFS)
- linux日志文件
- getchar和scanf的区别
- IOS Dev Intro - AVASSetWriterInput Scale Encode
- 如何使用自定义的签名进行调试
- HDU5726 (线段树+暴力)
- Android Studio创建的Android项目一般需要忽略
- 递归的logN的优化(菲波那契数列,青蛙上台阶问题,母牛问题)!!!!
- webview支持特效,页面内跳转