Farm Irrigation


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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()访问下个一元素即可,这里便是dfs思想的体现。我一开始想到的办法是,判断当前的字母是什么,比如说是A,然后再判断四个方向是否可以走:因为A的下和右没有水管指向,所以这两个方向肯定不可以走,然后判断左边是否是B或者C或者F或者G或者I或者J或者K(因为这几个字母都有指向左边的水管),有则可以从这个方向进行DFS。A的上方向同样处理。这样做下来之后,可以发现十分的不方便,要处理情况太多,代码量也将变得十分复杂(出现了很多的重复代码,几乎相当于用代码穷举出每个字母的每个方向是否可以走)。

  后来想到了一个比较巧妙的方法,其做法如下:

  声明一个如下的结构体

struct node
{
char ch;//该位置的字母
int flag;//用于判断该位置 是否遍历过
int a,b,c,d;//分别表示左、上、右、下是否有水管。0表示没有,1表示有
};
node src[][];

  那么如何快速判断当前位置(x,y)的下一个方向(xx,yy)是否可达呢(假设(xx,yy)未访问过且没有越界)?前面已经说过,(x,y)可以向左边走的条件是左边那小块农田有个指向右边的水管,所以,我们只要判断(x,y)的各方向是否可以走,可以这样判断:

  假设(xx,yy)为(x,y)的上边相邻元素:若(x,y).b*(xx,yy).d==1,则可从(x,y)往上走

  假设(xx,yy)为(x,y)的下边相邻元素:若(x,y).d*(xx,yy).b==1,则可从(x,y)往下走

  假设(xx,yy)为(x,y)的左边相邻元素:若(x,y).a*(xx,yy).c==1,则可从(x,y)往左走

   假设(xx,yy)为(x,y)的右边相邻元素:若(x,y).c*(xx,yy).a==1,则可从(x,y)往右走

  所以,dfs(int x,int y)的函数实现如下:

void dfs (int x,int y)
{
if(x<||x>=m||y<||y>=n) return ;//判断越界
src[x][y].flag=;
if(x->=&&src[x-][y].flag==&&src[x][y].b*src[x-][y].d==){//越界判断+访问标志判断+是否可走判断
dfs(x-,y);
}
if(x+<m&&src[x+][y].flag==&&src[x][y].d*src[x+][y].b==){
dfs(x+,y);
}
if(y->=&&src[x][y-].flag==&&src[x][y].a*src[x][y-].c==){
dfs(x,y-);
}
if(y+<n&&src[x][y+].flag==&&src[x][y].c*src[x][y+].a==){
dfs(x,y+);
}
return ;
}

  具备以上的了解,则求其最小连通度即可。

  该题目的完整C++实现代码如下:

 #include <iostream>
using namespace std;
int m,n;
struct node
{
char ch;
int flag;
int a,b,c,d;//分别表示左、上、右、下
}; node src[][]; void dfs (int x,int y)
{
if(x<||x>=m||y<||y>=n) return ;
src[x][y].flag=;
if(x->=&&src[x-][y].flag==&&src[x][y].b*src[x-][y].d==){
dfs(x-,y);
}
if(x+<m&&src[x+][y].flag==&&src[x][y].d*src[x+][y].b==){
dfs(x+,y);
}
if(y->=&&src[x][y-].flag==&&src[x][y].a*src[x][y-].c==){
dfs(x,y-);
}
if(y+<n&&src[x][y+].flag==&&src[x][y].c*src[x][y+].a==){
dfs(x,y+);
}
return ;
} int main()
{
while(cin>>m>>n){
if(m<||n<) break;
for(int i=;i<m;i++){
for(int j=;j<n;j++){
cin>>src[i][j].ch;
switch(src[i][j].ch){
case 'A':
src[i][j].a=;
src[i][j].b=;
src[i][j].c=;
src[i][j].d=;
break;
case 'B':
src[i][j].a=;
src[i][j].b=;
src[i][j].c=;
src[i][j].d=;
break;
case 'C':
src[i][j].a=;
src[i][j].b=;
src[i][j].c=;
src[i][j].d=;
break;
case 'D':
src[i][j].a=;
src[i][j].b=;
src[i][j].c=;
src[i][j].d=;
break;
case 'E':
src[i][j].a=;
src[i][j].b=;
src[i][j].c=;
src[i][j].d=;
break;
case 'F':
src[i][j].a=;
src[i][j].b=;
src[i][j].c=;
src[i][j].d=;
break;
case 'G':
src[i][j].a=;
src[i][j].b=;
src[i][j].c=;
src[i][j].d=;
break;
case 'H':
src[i][j].a=;
src[i][j].b=;
src[i][j].c=;
src[i][j].d=;
break;
case 'I':
src[i][j].a=;
src[i][j].b=;
src[i][j].c=;
src[i][j].d=;
break;
case 'J':
src[i][j].a=;
src[i][j].b=;
src[i][j].c=;
src[i][j].d=;
break;
case 'K':
src[i][j].a=;
src[i][j].b=;
src[i][j].c=;
src[i][j].d=;
break;
}
src[i][j].flag=;
}
}
int ans=;
for(int i=;i<m;i++){
for(int j=;j<n;j++){
if(src[i][j].flag==){
ans++;
dfs(i,j);
}
}
}
cout<<ans<<endl;
}
}

ZOJ 2412 Farm Irrigation的更多相关文章

  1. ZOJ 2412 Farm Irrigation(DFS 条件通讯块)

    意甲冠军  两个农田管内可直接连接到壳体  他们将能够共享一个水源   有11种农田  管道的位置高于一定  一个农田矩阵  问至少须要多少水源 DFS的连通块问题  两个相邻农田的管道能够直接连接的 ...

  2. HDU 2412 Farm Irrigation

    题目: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...

  3. ZOJ2412 Farm Irrigation(农田灌溉) 搜索

    Farm Irrigation Time Limit: 2 Seconds      Memory Limit: 65536 KB Benny has a spacious farm land to ...

  4. hdu.1198.Farm Irrigation(dfs +放大建图)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. HDU1198水管并查集Farm Irrigation

    Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...

  6. HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. 【简单并查集】Farm Irrigation

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  8. Farm Irrigation

    题目:Farm Irrigation 题目链接:http://210.34.193.66:8080/vj/Problem.jsp?pid=1494 题目思路:并查集 #include<stdio ...

  9. Farm Irrigation(非常有意思的并查集)

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

随机推荐

  1. SpringMVC注解@RequestParam全面解析---打酱油的日子

    在SpringMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取.这里主要 ...

  2. [超级懒人最简单法]iPhone 6 plus 适配切图方法分享(转载文章)

    网络上已经有很多适配教程,可是看了半天总是半懂不懂..最后还是要综合多个教程再动动脑子动动手,最好有程序大哥帮你试一下(这得有多大的福气) 如果有跟我一样情况的: 1.       有人说用sketc ...

  3. Bugtags 测试平台(支持ios、android)

    官网:https://bugtags.com/ 注意:小米手机 授权 打开漂浮窗 App 集成 Bugtags SDK 后,测试人员就可直接在 App 里所见即所得的提交 Bug; SDK 会自动截屏 ...

  4. requirejs 多页面,多js 打包代码,requirejs多对多打包【收藏】

    这段代码来自 http://stackoverflow.com/questions/20583812/grunt-requirejs-optimizer-for-a-multi-app-project ...

  5. 我JSP学习心得1

    老师布置了一项作业,说是要按着老师的要求写,但我觉得只要是技术分享的心得就是好的,不论是不是所要求的内容. 由于和几个人在外面给别人搭建网站,项目需要学习了jsp有用到了javascript,这里有一 ...

  6. Hibernate学习笔记3

    ---恢复内容开始--- 一.hibernate如何转化jdbc代码实例[通过hibernate构建jdbc后往数据库传对象] import java.sql.Connection;import ja ...

  7. Android 双卡双待识别

    简介 Android双卡双待已经越来越普及了,解决双卡双待管理是广大手机开发人员必须得面对的问题,为实现Android平台的双卡双待操作,笔者研究了Android 应用层操作双卡双待的机制. 机制 获 ...

  8. Java 对象拷贝方式

    (1)BeanUtils.cloneBean()使用: http://www.cnblogs.com/fervour/archive/2009/12/18/1627868.html package c ...

  9. tomcat下jsp要加工程名后缀才能访问的问题解决

    今天发现一个部署的项目,在tomcat中配置了去掉工程名,直接通过域名访问.配置后其它的html.动态请求等都可以不带工程名访问,但是只要访问jsp页面就报404错误,加上工程名访问jsp却又正常. ...

  10. DateUtil工具类

    package com.autoserve.mh.common.util;   import java.text.SimpleDateFormat; import java.util.Calendar ...