hdu 1198 Farm Irrigation 基础最小生成树★
来源:互联网 发布:java内存落差较大 编辑:程序博客网 时间:2024/06/05 08:22
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<stdio.h>#include<string.h>#include<map>#include<algorithm>using namespace std;char maps[103][103];int pre[10003];int n,m;struct data{ int x,y,number;} a[10003];void init(){ for(int i=0; i<=10000; i++) pre[i]=i;}int Find(int x){ if(x!=pre[x]) return pre[x]=Find(pre[x]); return x;}void Merge(int x,int y){ int X=Find(x); int Y=Find(y); if(X!=Y) pre[X]=Y;}/// up 1/// left 2int cnt=0;void judge(char c,int x,int y){ int x1=x-1,y1=y; int x2=x,y2=y-1; char a,b; if(x) a=maps[x1][y1]; if(y) b=maps[x2][y2]; if(x&&(c=='A'||c=='B'||c=='E'||c=='G'||c=='H'||c=='J'||c=='K')) if(a=='C'||a=='D'||a=='E'||a=='H'||a=='I'||a=='J'||a=='K') Merge(cnt,cnt-m); if(y&&(c=='A'||c=='C'||c=='F'||c=='G'||c=='H'||c=='I'||c=='K')) if(b=='B'||b=='D'||b=='F'||b=='G'||b=='I'||b=='J'||b=='K') Merge(cnt,cnt-1); return ;}int main(){ while(~scanf("%d%d",&n,&m)) { if(n==-1&&m==-1) break; init(); map<int,int>ma; memset(maps,'.',sizeof(maps)); for(int i=0; i<n; i++) { scanf("%s",maps[i]); } cnt=0; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { cnt++; judge(maps[i][j],i,j); } } int res=0; for(int i=1; i<=cnt; i++) { int X=Find(i); if(ma[X]!=1) ma[X]=1,res++; } printf("%d\n",res); }}
阅读全文
0 0
- hdu 1198 Farm Irrigation 基础最小生成树★
- 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
- 2015 ACM Amman Collegiate Programming Contest H.Bridges【边双联通+求树上最长链(树的直径)】
- 【miscellaneous】编码格式简介(ANSI、GBK、GB2312、UTF-8、GB18030和 UNICODE)
- 【物联网】矩阵式二维条码 介绍
- log4j2.xml路径的指定
- Java进阶之虚拟机《四》:类加载机制
- hdu 1198 Farm Irrigation 基础最小生成树★
- 进程/线程同步的方式和机制,进程间通信
- Dagger2
- PHP CURL模拟登陆并且上传文件
- python错误总结
- Html.RenderAction传递model
- .net MVC 数据库数据导出到Excel表格并下载到本地
- IntelliJ IDEA常用快捷键
- 死亡的意思就是没有任何意义