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

题目意思:有11种正方形农田,每种正方形农田里面对应一种形状的水管,不同的的正方形一用A到K表示,给一个矩阵,问至少需要多少个水源可以使矩形中所有的地方都可以被灌溉,如果两个相邻的正方形的
水管正好对口,那么这两个正方形可以共用一个水源。 解题思路:明显的DFS求连通区域,不过难点在于对农田水管的处理,我们可以开一个结构体数组,里面存放每一块农田四方向的水管,1代表有,0代表无。在搜索的过程中,假如当前的农田有向上的水管,而当前
农田的上面一块农田恰好有向下的水管,那么就可以从当前的水管向上搜索了。
 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
int maps[][];
int vis[][];
struct node
{
int up;
int down;
int left;
int right;
};
node num[]= {{,,,},{,,,},{,,,},{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,},{,,,},{,,,}};
void DFS(int x,int y)
{
int i;
int a,b;
if(x<=||x>m||y<=||y>n||vis[x][y])
{
return ;
}
vis[x][y]=;
for(i=; i<; i++)
{
if(i==)///向上走
{
a=x-;
b=y+;
if(num[maps[x][y]].up&&num[maps[a][b]].down)
{
DFS(a,b);
}
}
else if(i==)///向下走
{
a=x+;
b=y+;
if(num[maps[x][y]].down&&num[maps[a][b]].up)
{
DFS(a,b);
}
}
else if(i==)///向左走
{
a=x+;
b=y-;
if(num[maps[x][y]].left&&num[maps[a][b]].right)
{
DFS(a,b);
}
}
else if(i==)///向右走
{
a=x+;
b=y+;
if(num[maps[x][y]].right&&num[maps[a][b]].left)
{
DFS(a,b);
}
}
}
return ;
}
int main()
{
char ch;
int i,j;
int counts;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(vis,,sizeof(vis));
if(m==-&&n==-)
{
break;
}
getchar();
counts=;
for(i=; i<=m; i++)
{
for(j=; j<=n; j++)
{
scanf("%c",&ch);
maps[i][j]=ch-'A';
}
getchar();
} for(i=; i<=m; i++)
{
for(j=; j<=n; j++)
{
if(!vis[i][j])
{
counts++;
DFS(i,j);
}
}
}
printf("%d\n",counts);
}
return ;
}

Farm Irrigation ZOJ 2412(DFS连通图)的更多相关文章

  1. HDU 1198 Farm Irrigation(状态压缩+DFS)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目: Farm Irrigation Time Limit: 2000/1000 MS (Ja ...

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

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

  3. ZOJ 2412 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)T ...

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

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

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

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

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

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

  8. HDUOJ--------(1198)Farm Irrigation

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

  9. HDU1198水管并查集Farm Irrigation

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

随机推荐

  1. Ajax数据获取(未完待续。。。)

    @:网站内容通过Ajax获取数据然后重新渲染产生 这种时候我们如何拿到自己想要的数据: 1:打开检查,在network选项中查看浏览器发送了哪些请求.(清空,再刷新,得到新的请求) 在截图的左下角可以 ...

  2. 报错: WARN hdfs.DFSClient: Caught exception java.lang.InterruptedException

    WARN hdfs.DFSClient: Caught exception java.lang.InterruptedException 而且日志中没有错误. 官网语句:$ bin/hdfs dfs ...

  3. Fiddler抓取手机APP数据包

    第一步:下载神器Fiddler,下载链接: http://w.x.baidu.com/alading/anquan_soft_down_ub/10963 下载完成之后,傻瓜式的安装一下了! 第二步:设 ...

  4. 【Mac】gem install 出错 You don't have write permissions for the /Library/Ruby/Gems

    问题描述 RedisDump 是一个用于 Redis 数据导人/导出的工具,是基于 Ruby 实现的,需要先安装 Ruby.但因为 Mac 自带有 Ruby 所以我直接用gem install red ...

  5. 基于visual studio 2017 以及cubemx 搭建stm32的开发环境(0)

    (1)安装visual studio 2017 官网下载安装即可 (2)安装visual GDB 链接:https://pan.baidu.com/s/1TgXI1BRQLAWiWlqCcIS9TA ...

  6. Verilog的一些系统任务(二)

    $monitor 任务$monitor提供了监控和输出参数列表中的表达式或变量值的功能. 格式: $monitor(p1,p2,...,pn);   $monitor;        $monitor ...

  7. java入门---修饰符&访问修饰符&非访问修饰符

        Java语言提供了很多修饰符,主要分为以下两类: 访问修饰符 非访问修饰符     修饰符用来定义类.方法或者变量,通常放在语句的最前端.我们通过下面的例子来说明: public class ...

  8. 20155209实验二《Java面向对象程序设计》

    20155209实验二<Java面向对象程序设计> 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计 ...

  9. 20155229 实验一《Java开发环境的熟悉》实验报告

    20155229 实验一<Java开发环境的熟悉>实验报告 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Idea 编辑.编译.运行.调试Java程序. 实验步骤 (一) ...

  10. 2016-2017-2 20155331 实验二《Java面向对象程序设计》实验报告

    2016-2017-2 20155331 实验二<Java面向对象程序设计>实验报告 实验内容 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装.继承.多态 3.初步掌握U ...