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. 使用yum来下载RPM包而不进行安装

    1. 安装yum-downloadonly. yum-utils 或 yum-plugin-downloadonly 软件包 (RHEL5) # yum install yum-downloadonl ...

  2. 【c语言】求最大值

    一.我个人觉得求最大值比较简单的一种方法(当然同时求最大值和最小值时稍微改改也能行) #include <stdio.h> int main(void) { int f, i, max; ...

  3. (转)requirejs:杏仁的优化(almond)

    使用场景 什么情况下需要使用 almond 呢?假设你手头有个基于requirejs的小项目,所有业务代码加起来就几十K(压缩后可能更小).出于性能优化的考虑,你可能在想:如果能够去掉requirej ...

  4. QT 遍历目录查找指定文件(比较简单)

    QString FindFile(const QString &strFilePath, const QString &strNameFilters){ if (strFilePath ...

  5. 「JAVA」:Berkeley DB的JAVA连接

    Berkeley DB是一个嵌入式的数据库,它适合于管理海量的.简单的数据.关键字/数据(key/value)是Berkeley DB用来进行数据管理的基础.每个key/value构成了一条记录,而整 ...

  6. 解决安装oracle后系统变慢问题

    Oracle数据库是一个很占资源的软件,光一个实例服务所占内存,根据其安装时分配的内存就至少要达到256MB以上,再加上其他附属服务,光内存就要占用物理内存的400M左右,虚拟内存也会有等值或更高的损 ...

  7. linux进程解析--进程切换

    为了控制进程的执行,linux内核必须有能力挂起正在cpu上运行的进程,换入想要切换的进程,也就是恢复以前某个挂起的进程,这就是linux的进程切换.  1进程切换的时机 一般来说,进程切换都是发生在 ...

  8. 暂时和永久改动oracle sysdate的默认输出格式

    1.当前会话有效 alter session set NLS_DATE_FORMAT='YYYY-MM-DD:HH24:MI:SS'; 2.永久生效 sys用户登入后运行例如以下命令 然后重新启动数据 ...

  9. css中的定位

    上一篇博客,我大概介绍了下浮动的使用及行为.其实在整个文档布局中,定位也对我们整个的页面排版有非常好的帮助,当然前提是使用得当. 一.定位分类: a.静态定位  position:static;   ...

  10. 【Web】java异常处理

    J2EE中一般对异常状况的处理都可以用两种情况对其进行相应处理. 1. 通常情况下,一般异常处理可以选择用throw.throws从底层一直往上面抛,直到抛到Action,让其将异常显示在页面上面进行 ...