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 ;
}

最新文章

  1. 【转载】C#怎么判断字符是不是汉字
  2. Swift学习--常量.变量.数据类型的使用(二)
  3. C#: log4net
  4. C++静态成员函数访问非静态成员的几种方法
  5. Log4net 可直接使用的配置
  6. iPhone的刷机 iPhone进UDF
  7. Android初级教程调用手机拍照与摄像功能
  8. 在DOS命令中输入ipconfig /all,出现“该命令不是系统内部命令......”
  9. Intellij idea使用过程中遇到的一些问题
  10. 剑指Offer 15. 反转链表 (链表)
  11. MT【184】$\epsilon$助力必要性
  12. How to convert String to Date – Java
  13. JS的增删改查
  14. actor model vs tasked based parallizm
  15. Linq简单使用
  16. js 常用脚本
  17. android 加固防止反编译-重新打包
  18. (转)iptables简介
  19. 漫话最小割 part1
  20. C#实现如何判断一个数组中是否有重复的元素 返回一个数组升序排列后的位置信息--C#程序举例 求生欲很强的数据库 别跟我谈EF抵抗并发,敢问你到底会不会用EntityFramework

热门文章

  1. 832. Flipping an Image (5月22日 )
  2. Hadoop相关笔记
  3. Quick find Helper
  4. hadoop docker集群搭建
  5. s3c2440存储控制器详解
  6. 自学tensorflow——1.框架初步了解以及构建简单的计算图计算
  7. Python编程小坑
  8. 使用JSTL的taglib做if判断
  9. 目标反射回波检测算法及其FPGA实现 之一:算法概述
  10. Pytorch之认识Variable