HDU 1198 Farm Irrigation
来源:互联网 发布:cmd关闭端口命令 编辑:程序博客网 时间:2024/06/15 03:36
Farm Irrigation
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 33 Accepted Submission(s) : 21
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
Author
Source
Zhejiang University Local Contest 2005
合并之前先判断是否能连通,若能连通则合并,不能连通,则不能合并。
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<climits>#include<string>#include<queue>#include<stack>#include<algorithm>using namespace std;#define rep(i,j,k)for(i=j;i<k;i++)#define per(i,j,k)for(i=j;i>k;i--)#define MS(x,y)memset(x,y,sizeof(x))typedef long long LL;const int INF=0x7ffffff;const int M=50+1;//将二维坐标转换成一维的标号,此处用行优先int pre[M*M+1];char farm[M][M];int i,j,k,n,m,cnt;//存储11中类型的土地,二维中的0 1 2 3分别代表这种类型的土地的左上右下//为1表示这个方向有接口,为0表示这个方向没有接口int type[11][4]={{1,0,1,0},{1,0,0,1}, {0,1,1,0},{0,1,0,1}, {1,1,0,0},{0,0,1,1}, {1,0,1,1},{1,1,1,0}, {0,1,1,1},{1,1,0,1}, {1,1,1,1}};void init(int n){ rep(i,0,n+1) pre[i]=i;}int root(int x){ if(x!=pre[x]) pre[x]=root(pre[x]); return pre[x];}void combine(int sx,int sy,int ex,int ey,int dir){ if(ex>n||ey>m)return; bool mark=false; int ta=farm[sx][sy]-'A'; int tb=farm[ex][ey]-'A'; if(dir) {if(type[ta][3]&&type[tb][2])mark=true;} else {if(type[ta][1]&&type[tb][0])mark=true;} if(mark){ int fx=root((sx-1)*m+sy); int fy=root((ex-1)*m+ey); if(fx!=fy)pre[fx]=fy,--cnt; }}int main(){ while(~scanf("%d%d",&n,&m)) { if(n==-1&&m==-1)break; init(n*m); rep(i,1,n+1)scanf("%s",farm[i]+1); cnt=n*m; rep(i,1,n+1) rep(j,1,m+1){ combine(i,j,i+1,j,0); combine(i,j,i,j+1,1); } printf("%d\n",cnt); } return 0;}
0 0
- HDU 1198 Farm Irrigation
- hdu 1198 Farm Irrigation
- hdu 1198 Farm Irrigation
- HDU--1198Farm Irrigation
- hdu 1198 Farm Irrigation
- HDU 1198-Farm Irrigation
- HDU 1198 Farm Irrigation
- hdu 1198 Farm Irrigation
- hdu 1198 Farm Irrigation
- HDU 1198 Farm Irrigation
- HDU 1198 Farm Irrigation
- HDU 1198 Farm Irrigation
- hdu 1198 Farm Irrigation
- hdu 1198 Farm Irrigation
- hdu 1198 Farm Irrigation
- hdu 1198 Farm Irrigation
- hdu(1198)Farm Irrigation
- hdu 1198 Farm Irrigation
- ROT13函数
- HTTP连接管理
- Ubuntu 14.04 安装 Grub Customizer 4.0.6
- Bellman_Ford算法
- LintCode:组合
- HDU 1198 Farm Irrigation
- Kafka删除topic
- Android开发---AUIL图片加载框架的使用
- 冰点还原精灵如何破载?
- python中的多线程----以2个线程卖票为例
- HTTP缓存详解
- TOJ 1320.Billiard
- python核心编程学习笔记-2016-08-21-02-python网络编程TCP
- mybatis总结