hdu1198
2015-08-09 18:06
295 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1198
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7199 Accepted Submission(s): 3088
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.
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
Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7199 Accepted Submission(s): 3088
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.
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 用 dfs搞的#include <iostream> #include <stdio.h> #include <string.h> using namespace std; struct ac { int l,r,u,d; }d[20]; int n,m; char str[55][55]; int a[55][55],b[55][55]; void inti() { d[1].l=1,d[1].r=0,d[1].u=1,d[1].d=0; d[2].l=0,d[2].r=1,d[2].u=1,d[2].d=0; d[3].l=1,d[3].r=0,d[3].u=0,d[3].d=1; d[4].l=0,d[4].r=1,d[4].u=0,d[4].d=1; d[5].l=0,d[5].r=0,d[5].u=1,d[5].d=1; d[6].l=1,d[6].r=1,d[6].u=0,d[6].d=0; d[7].l=1,d[7].r=1,d[7].u=1,d[7].d=0; d[8].l=1,d[8].r=0,d[8].u=1,d[8].d=1; d[9].l=1,d[9].r=1,d[9].u=0,d[9].d=1; d[10].l=0,d[10].r=1,d[10].u=1,d[10].d=1; d[11].l=1,d[11].r=1,d[11].u=1,d[11].d=1; } void dfs(int x,int y) { b[x][y]=1; int nx,ny; nx=x+1; ny=y; if(nx<n&&nx>=0&&ny>=0&&ny<m&&b[nx][ny]==0) if(d[a[nx][ny]].u==1&&d[a[x][y]].d==1) dfs(nx,ny); nx=x-1; ny=y; if(nx>=0&&ny>=0&&nx<n&&ny<m&&b[nx][ny]==0) if(d[a[nx][ny]].d==1&&d[a[x][y]].u==1) dfs(nx,ny); nx=x; ny=y+1; if(ny<m&&b[nx][ny]==0&&ny>=0&&nx>=0&&nx<n) if(d[a[x][y]].r==1&&d[a[nx][ny]].l==1) dfs(nx,ny); nx=x; ny=y-1; if(ny>=0&&b[nx][ny]==0&&ny<m&&nx>=0&&nx<n) if(d[a[x][y]].l==1&&d[a[nx][ny]].r==1) dfs(nx,ny); return; } int main() { inti(); while(scanf("%d%d",&n,&m)&&n!=-1&&m!=-1) { for(int i=0;i<n;i++) { scanf("%s",str[i]); for(int j=0;j<m;j++) { a[i][j]=str[i][j]-'A'+1; b[i][j]=0; //cout<<a[i][j]<<" "; } } // memset(b,0,sizeof(b)); int ans=0; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(b[i][j]==0) { dfs(i,j); ans++; } } cout<<ans<<endl; } return 0; }
相关文章推荐
- [转]使用Cadence ADE + Spectre做Montel Carlo仿真
- XML Schema命名空间:xmlns介绍
- C语言中的__FILE__、__LINE__和#line
- Java CAS 和ABA问题
- Codeforces548C:Mike and Frog
- Java记录 -5- 运算符续 Operator
- tarjan算法(边的双连通分量)
- Eclipse背景设置
- Leetcode: Sqrt(x)
- Nim中文件IO
- 分治算法排序(C++版)
- form表单提交到servlet显示HTTP Status 404错误The requested resource is not available.
- discuz和windPhp的邮件设置
- centos开启防火墙
- [Vijos]P1062 迎春舞会之交谊舞
- [Asp.net]缓存简介
- 函数之间传参,获取别的函数内的变量值
- 操作大文本数据存储在数据库中 mysql中有个lob
- ios 一次性代码的实现
- ITK/VTK对DICOM文件的读取