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. 第一个processing程序(2016-01-15)

    前几天下载和安装了最新的 processing,今天试一下,哈哈,真是简单之极啊,果然是给非程序员使用的,现在,我也是非程序员了.

  2. ibatis缓存配置

    一.sqlmapconfig.xml <sqlMapConfig> <settings useStatementNamespaces="true"  cacheM ...

  3. Spring注解配置

    配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  4. js 完成单继承

    //1.使用prototype完成单继承. //定义一个A类 function A(){ } //为A类动态调用属性color,与方法sayColor A.prototype.color = &quo ...

  5. Sort list by merge sort

    使用归并排序对链表进行排序 O(nlgn) 的时间效率 /** * Definition for singly-linked list. * struct ListNode { * int val; ...

  6. NET Core1

    NET Core .net core最近园子讨论频率很高的话题,从不久前发布正式版本后,也是开始从netcore官网一步一步走向学习之路:.net跨平台的设计让人很是兴奋起来,因为做了多年的互联网研发 ...

  7. 一个i++和++i导致的严重的错误

    当我曾经在写一个strlen的实现时,用递归写出了如下的代码: int strlen(const char *s) { if(*s=='\0') ; else ; } 程序一运行就崩溃了,why!都是 ...

  8. java学习之网络编程之echo程序

    服务端的实现 package com.gh.echo; import java.io.*; import java.net.*; /** * echo服务器程序 * 实现 不断接收字符串 ,然后返回一 ...

  9. H面试程序(16): 简单选择排序

    #include<stdio.h> #include<assert.h> void display(int * a, int n) { assert(a); for(int i ...

  10. iOS 把图片从Mac本地添加到iOS Simulator中

    [把图片从Mac本地添加到iOS Simulator中] 1. 把图片从Mac本机拖动到iOS Simulator中: 2. iOS Simulator会自动打开Safari去打开对应的图片,然后你用 ...