题目链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2412

题目:

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.

<b< dd="">

Output

For each test case, output in one line the least number of wellsprings needed.

<b< dd="">

Sample Input

2 2
DK
HF 3 3
ADC
FJK
IHE -1 -1

<b< dd="">Sample Output

2
3

题意描述:

输入矩阵的大小和字母表示的矩阵,表示不同型号的水管摆成的形状

解题思路:
题目还是很有意思的,实际就是找找有几个独立的连通区域。将对应字母型号的水管四个方向的状态存进数组,使用广搜遍历每个水管,判断在其方向上的水管的对应方向的状态即可。

代码实现:

 #include<stdio.h>
#include<string.h>
char map[][];
int m,n,book[][];
void bfs(int x,int y,int c);
struct n
{
int x,y;
};
int main()
{
int i,j,c;
while(scanf("%d%d",&m,&n), n!=- && m!= -)
{
for(i=;i<m;i++)
for(j=;j<n;j++)
scanf(" %c",&map[i][j]); memset(book,,sizeof(book));
for(c=,i=;i<m;i++){
for(j=;j<n;j++){
if(book[i][j]==)
bfs(i,j,++c);
}
}
printf("%d\n",c);
}
return ;
}
void bfs(int sx,int sy,int c)
{
struct n q[];
int i,tx,ty,head=,tail=,k;
int next[][]={,, ,, ,-, -,};
int list[]={,,,,,}; int alp[][]={,,,, ,,,, ,,,, ,,,, ,,,, ,,,,
,,,, ,,,, ,,,, ,,,, ,,,};//A到K的水管,每4个数表示一个水管,分别是右、下、左、上的状态
q[tail].x=sx;
q[tail].y=sy;
tail++;
book[sx][sy]=c;
while(head<tail)
{
for(k=;k<=;k++)
{
tx=q[head].x + next[k][];
ty=q[head].y + next[k][];
if(tx< || tx>=m || ty< || ty>=n)//设置边界
continue;
if(alp[ map[ q[head].x ][ q[head].y ]-'A' ][list[k]] && alp[ map[tx][ty]-'A' ][ list[k+] ] && !book[tx][ty])
{//对准位置
book[tx][ty]=c;
q[tail].x=tx;
q[tail].y=ty;
tail++;
}
}
head++;
}
}

易错分析:

1、注意遍历方向的时候要对准每个水管的对应方向的状态

2、边界设置要准确

最新文章

  1. java中this 关键字的使用
  2. Android开发环境搭建全程演示(jdk+eclipse+android sdk)
  3. Input 值改变触发事件
  4. 边工作边刷题:70天一遍leetcode: day 71-3
  5. Android自定义进度条
  6. 《算法问题实战策略》-chaper15-计算几何-线段相交
  7. ZFS+Dtrace+Zones+KVM=SMARTOS + dtrace 详细文档
  8. android分享到新浪微博,认证+发送微博,
  9. Twisted No module named win32api
  10. STL 源代码剖析 算法 stl_algo.h -- merge sort
  11. iOS苹果官方Demo合集
  12. python中数字类型与处理工具
  13. [ExtJS6学习笔记]Ext JS6主题系列 (Classic工具包)
  14. javascript事件委托的原理与实现
  15. php中连接mysql数据库的第一步操作
  16. php使用fullcalendar日历插件
  17. Spark Client启动原理探索
  18. Django之模型层-多表操作
  19. SVN Error: Unreadable path encountered; access denied;
  20. Go 入门 - 包,函数和变量

热门文章

  1. 01.阿里云SDK调用,获取ESC主机详细信息
  2. Visual studio code离线安装插件
  3. 【开源】AspnetCore 2.0 自动API文档生成组件,支持protobuffer
  4. CRM项目总结
  5. Head First设计模式之工厂模式
  6. swift4.0 正则表达式判断手机号
  7. Hibernate学习笔记(4)---hibernate的核心接口
  8. micropython TPYBoard v202 超声波测距
  9. Java学习笔记8(面向对象一:概念、private)
  10. 2018年1月 mybatis+ 动态sql连续日期