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<stdio.h>
#include<iostream>
using namespace std;
typedef struct nn
{
int d[4];//按顺序左,上,右,下;0表示不路通,1表示路通
}node;
node map[55][55],N[11];
int n,m,vist[55][55],dir[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
void set_N()
{
for(int i=0;i<11;i++)
{
if(i==0){N[i].d[0]=N[i].d[1]=1;N[i].d[2]=N[i].d[3]=0;}
if(i==1){N[i].d[1]=N[i].d[2]=1;N[i].d[0]=N[i].d[3]=0;}
if(i==2){N[i].d[0]=N[i].d[3]=1;N[i].d[2]=N[i].d[1]=0;}
if(i==3){N[i].d[2]=N[i].d[3]=1;N[i].d[1]=N[i].d[0]=0;}
if(i==4){N[i].d[1]=N[i].d[3]=1;N[i].d[2]=N[i].d[0]=0;}
if(i==5){N[i].d[0]=N[i].d[2]=1;N[i].d[1]=N[i].d[3]=0;}
if(i==6){N[i].d[0]=N[i].d[1]=N[i].d[2]=1;N[i].d[3]=0;}
if(i==7){N[i].d[0]=N[i].d[1]=N[i].d[3]=1;N[i].d[2]=0;}
if(i==8){N[i].d[0]=N[i].d[2]=N[i].d[3]=1;N[i].d[1]=0;}
if(i==9){N[i].d[1]=N[i].d[3]=N[i].d[2]=1;N[i].d[0]=0;}
if(i==10){N[i].d[0]=N[i].d[3]=N[i].d[2]=N[i].d[1]=1;}
}
}
void dfs(int x,int y)
{
int tx,ty;
vist[x][y]=1;
for(int e=0;e<4;e++)
if(map[x][y].d[e])
{
tx=x+dir[e][0];ty=y+dir[e][1];
if(!vist[tx][ty]&&tx>=0&&tx<n&&ty>=0&&ty<m)
{
if(e==0&&map[tx][ty].d[2]||e==1&&map[tx][ty].d[3])
dfs(tx,ty);
if(e==2&&map[tx][ty].d[0]||e==3&&map[tx][ty].d[1])
dfs(tx,ty);
}
}
}
int main()
{
char c;
int k;
set_N();
while(scanf("%d%d",&n,&m)>0&&n+m!=-2)
{
for(int i=0;i<n;i++)
{
getchar();
for(int j=0;j<m;j++)
{
cin>>c;
map[i][j]=N[c-'A'];
vist[i][j]=0;
}
} k=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(!vist[i][j])
{
k++;
dfs(i,j);
}
printf("%d\n",k);
}
}

hdu1198Farm Irrigation (DFS)的更多相关文章

  1. hdu1198Farm Irrigation(dfs找联通)

    题目链接: 啊哈哈,选我选我 思路是:首先依据图像抽象出联通关系.. 首先确定每一种图形的联通关系.用01值表示不连通与不连通... 然后从第1个图形进行dfs搜索.假设碰到两快田地能够联通的话那么标 ...

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

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

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

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

  4. hdu1198 Farm Irrigation —— dfs or 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 dfs: #include<cstdio>//hdu1198 dfs #includ ...

  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)T ...

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

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

  8. (DFS)hdoj1198-Farm Irrigation

    题目链接 DFS的简单应用,比较繁琐的是处理输入的英文字母.用并查集也可以做(可是笔者现在还没有掌握并查集,之前只用过一次,以后学会回来补上) #include<cstdio> #incl ...

  9. hdu 1198 (并查集 or dfs) Farm Irrigation

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...

随机推荐

  1. python编写工具及配置(notepad++)

    学长跟我说老师实验室里用的ide是pycharm,我用了一天,整体还行,就是加载速度太慢,可是第二天用的时候就卡的想让人骂街,cpu占有率趋近100%,电脑配置不高,我寻思不能因为这个就马上换电脑吧, ...

  2. TextView实现多个TextView对象的走马灯效果

    1:自定义一个控件继承TextView,重写isFocused方法,返回值为true; package com.example.helloandroid; import android.R.bool; ...

  3. codeforces 478B Random Teams

    codeforces   478B  Random Teams  解题报告 题目链接:cm.hust.edu.cn/vjudge/contest/view.action?cid=88890#probl ...

  4. java集群

    java集群 分类: java学习2011-05-12 09:12 7531人阅读 评论(9) 收藏 举报 java服务器负载均衡ejb集群数据库 序言 越来越多的关键应用运行在J2EE(Java 2 ...

  5. ThinkPHP第十天(_initialize方法,SESSION销毁,分组配置,include文件引入,JOIN用法)

    1.Action类中的_initialize()函数,先于任何自定义操作函数运行,可认为是控制器的前置操作.可用于检测用户是否登录等检测. 如果多个模块(Action)需要相同_initialize( ...

  6. linux-2.6.33移植到FL2440

    宿主机:ubuntu10.04 目标机:fl2440 交叉编译器:arm-linux-gcc-3.4.1 交叉编译器路径:/usr/local/arm/3.4.1 要移植的内核版本:linux-2.6 ...

  7. MongoDB入门(1)--安装配置

    第一步:下载安装 首先当然是找到官方网站http://www.mongodb.org/ 进入下载页面 可以看到,当前最新版本是2.4.5,我的电脑是64位的win7,所以要下载第一个(说明一下,第二个 ...

  8. MFC的最大化,最小化,关闭

    最大化.最小化和关闭按钮是窗口中最主要的元素.首先要说明,说他们是按钮其实是不准确的,按钮是一种窗口,而这三个组件根本就不是窗口,而是一个窗口常见的组成部分.出于习惯的原因,这里还是称呼他们为按钮. ...

  9. perl 自动发产品

    use Net::SMTP; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Headers; use HTTP::Response; use Enc ...

  10. 基于visual Studio2013解决C语言竞赛题之0402奇偶求和

      题目 解决代码及点评 这道题考察我们对循环和判断的综合应用 #include <stdio.h> #include <stdlib.h> #include < ...