ZOJ 2412 Farm Irrigation
2015-09-05 16:46
302 查看
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
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
Sample Output
限定条件的BFS 略坑 WA好多发 细节掌握得不行
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
限定条件的BFS 略坑 WA好多发 细节掌握得不行
#include <stdio.h> #include <string.h> #include <iostream> #include <queue> using namespace std; char map[55][55]; int use[55][55]; int n,m,d[5][2]={{0,0},{-1,0},{0,1},{1,0},{0,-1}}; struct pp { int x,y; }; queue<pp>q; int pd(char c,int b) { if(c=='A' && (b==2||b==3)) return 0; else if(c=='B'&&(b==3||b==4)) return 0; else if(c=='C'&&(b==1||b==2)) return 0; else if(c=='D'&&(b==1||b==4)) return 0; else if(c=='E'&&(b==2||b==4)) return 0; else if(c=='F'&&(b==1||b==3)) return 0; else if(c=='G'&&b==3) return 0; else if(c=='H'&&b==2) return 0; else if(c=='I'&&b==1) return 0; else if( (b==4 && c=='J')) return 0; return 1; } int pd1(char e,char a,int b) { if(b==1 && pd(e,b) && (a=='C'||a=='D'||a=='H'||a=='I'||a=='E'||a=='J'||a=='K')) return 1; if(b==2 && pd(e,b) && (a=='A'||a=='C'||a=='F'||a=='H'||a=='I'||a=='K'||a=='G')) return 1; if(b==3 && pd(e,b) && (a=='A'||a=='B'||a=='E'||a=='G'||a=='H'||a=='J'||a=='K')) return 1; if(b==4 && pd(e,b) && (a=='B'||a=='D'||a=='F'||a=='G'||a=='I'||a=='J'||a=='K')) return 1; return 0; } void bfs(int i,int j) { pp a,now,next; a.x=i; a.y=j; q.push(a); while(!q.empty()) { now=q.front(); q.pop(); for(i=1;i<=4;i++) { next.x=now.x+d[i][0]; next.y=now.y+d[i][1]; if( next.x<1|| next.x>n ||next.y<1|| next.y>m || !use[next.x][next.y] ) //没用过 continue; if(!pd1(map[now.x][now.y],map[next.x][next.y],i))//能连通 continue; use[next.x][next.y]=0; q.push(next); } } } int main() { int i,j,sum; while(~scanf("%d %d",&n,&m)) { if(n<=0||m<=0) break; sum=0; for(i=1;i<=n;i++) cin>>map[i]+1; memset(use,1,sizeof use); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(use[i][j]) { use[i][j]=0; bfs(i,j); sum++; } } } printf("%d\n",sum); } return 0; }
相关文章推荐
- Java垃圾回收
- Android 动态设置控件获取焦点
- 修改MySQL的数据库引擎为INNODB的方法
- Oracle安装介质及补丁集下载地址
- Android 动态设置控件获取焦点
- 3.2.5.3 search()函数和match()函数区别
- STL容器 — 顺序容器
- 3.2.5.3 search()函数和match()函数区别
- 亚稳态的概念
- 新系統必須安裝的軟件列表
- 三维凸包模板
- 【暑假的读书声】—2014.7.1至2015.9.1
- 排序算法时间复杂度对比
- vuGen回放https录制出错
- Design + Code (iOS)
- TOYS - POJ 2318(计算几何,叉积判断)
- web性能优化
- 【c++】大牛解析虚函数表
- linux 扩展源epel
- 黑马程序员----oc基础笔记----多态