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. react-router里使用history

    import React, { Component } from 'react';import {Router, Route, Switch} from 'react-router-dom';impo ...

  2. yyy loves Easter_Egg I(恶心的字符串模拟)

    题目背景 Soha的出题效率着实让人大吃一惊.OI,数学,化学的题目都出好了,物理的题还没有一道.于是,Huntfire,absi2011,redbag对soha进行轮番炸,准备炸到soha出来,不料 ...

  3. laychat聊天功能

    windows版本:1.直接下载laychat聊天室压缩包,并解压到PHPstudy本地PHP环境中去:2.进入E:\PHPTutorial\WWW\laychat-master\vendor\Wor ...

  4. pycharm社区版新建django文件不友好操作

    一.cmd操作 1.django-admin startproject (新建project名称) 2.在pycharm打开project,运行终端输入:python manage.py starta ...

  5. 基于STM32F103ZET6 HC_SR501人体红外感应

    这是最后的实验现象,有人走过会一直输出有人,离开范围时则输出没人 开发板 PZ6086L ,HC_SR501模块 这是HC_SR501的示意图,,VCC和GND不再多做介绍,5V供电就行, OUT接口 ...

  6. 重学Veriliog(2)——高级编程语句

    1.判断 1.1 if ... else ... 有优先级 在组合逻辑电路中,需要避免产生Latch(避免结构不完整) Latch容易引起竞争冒险,同时静态时序分析工具也不好分析穿过Latch的路径? ...

  7. DataTable 递归 简单的程序,来实现无限级列表 结合 jquery.table.js 实现

    protected void DiGuiDataTable(DataTable FromDataTable, DataTable ToDataTable, object pid) { ) { fore ...

  8. 20155234 2016-2017-2 《Java程序设计》第1 周学习总结

    20155234 2016-2017-2 <Java程序设计>第1 周学习总结 教材学习内容总结 第一周学习了第一章,第一章的内容等同于绪论,向我们介绍了jave的前世今生,以及三大平台. ...

  9. 20155313 2016-2017-2 《Java程序设计》第三周学习总结

    20155313 2016-2017-2 <Java程序设计>第三周学习总结 教材学习内容总结 4.1 类与对象 4.1.1 定义类 书本中使用了设计衣服来定义类,一件衣服的设计Cloth ...

  10. oracle基础命令

    oracle使用步骤: 一.oracle安装 两个文件解压到同一文件夹,doc为说明/使用文档 二.oracle启动: 1.启动oracle:启动监听和自定义库 2.启动cmd->sqlplus ...