Farm Irrigation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4802    Accepted Submission(s): 2073

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
 
题目大意:正方型的田,若相邻的田水管接口可以相连,则他们之间联通。统计联通子图的个数。
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 55
using namespace std; int n,m,f[N*N];
int map[][];
int dir[][]={{,-},{-,},{,},{,}}; int farm[][]={
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,}
}; void init(){ for(int i=; i<N*N; ++i)f[i]=i;} int findset(int x){ return f[x]!=x?f[x]=findset(f[x]):f[x];} void merge(int x,int y)
{
int a=findset(x), b=findset(y);
if(a==b)return ;
if(a<b) f[a]=b;
else f[b]=a;
} int main()
{
int i,j,k,dx,dy;
char ch;
while(scanf("%d%d%",&n,&m))
{
if(n==- && m==-) break;
for(i=; i<n; ++i)
{
for(j=; j<m; ++j)
{
scanf("%c",&ch);
map[i][j]=ch-'A';
}
getchar();
}
init();
for(i=; i<n; ++i)
{
for(j=; j<m; ++j)
{
for(k=; k<; ++k)
{
dx=i+dir[k][],dy=j+dir[k][];
if(dx<||dx>=n||dy<||dy>=m)continue;
if(k==)// 左
{
if(farm[map[dx][dy]][]&&farm[map[i][j]][]){
merge(dx*m+dy, i*m+j);
}
}
else if(k==)// 上
{
if(farm[map[dx][dy]][]&&farm[map[i][j]][]){
merge(dx*m+dy, i*m+j);
}
}
else if(k==)// 右
{
if(farm[map[dx][dy]][]&&farm[map[i][j]][]){
merge(dx*m+dy, i*m+j);
}
}
else if(k==)// 下
{
if(farm[map[dx][dy]][]&&farm[map[i][j]][]){
merge(dx*m+dy, i*m+j);
}
}
}
}
}
int cnt=;
for(i=; i<n*m; ++i)
if(f[i]==i)
++cnt;
printf("%d\n", cnt);
}
return ;
}
 

最新文章

  1. 达夫设备(Duff&#39;s Device)
  2. EF里的默认映射以及如何使用Data Annotations和Fluent API配置数据库的映射
  3. php之JavaScript
  4. iOS 随记
  5. app设计需注意的
  6. C# 循环语句 for循环
  7. IT男的”幸福”生活&quot;续7
  8. android实现可拖动按钮
  9. grep简单常用的语法介绍
  10. Away3d 骨骼动画优化
  11. ASP.NET Core Authorization
  12. JPG、PNG和GIF图片的基本原理及优化方法
  13. vertx的HttpServer模块
  14. Android greenDAO 数据库 简单学习之基本使用
  15. 关于单元测试时加载spring-context.xml文件的问题
  16. python数据结构与算法第九天【选择排序】
  17. 第十八单元 nginx服务
  18. jquery中ajax的写法
  19. HDU 4725 The Shortest Path in Nya Graph(构图)
  20. 嵌入式C编程代码优化笔记

热门文章

  1. 51nod 算法马拉松17 解题报告 以后不能赛中写题解(查逐梦者抄袭本人代码...
  2. plsql循环的简单实例
  3. javaEE(15)_Servlet过滤器
  4. C# 读App.config配置文件[2]: .Net Core框架
  5. CSS3的border-image
  6. 【java】 field 和 variable 区别及相关术语解释
  7. 【php】 自带的过滤机制
  8. 面向对象之多态,property
  9. appIcon
  10. LeetCode(99) Recover Binary Search Tree