题目链接:

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、边界设置要准确

HDU 2412 Farm Irrigation的更多相关文章

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

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

  2. ZOJ 2412 Farm Irrigation

    Farm Irrigation Time Limit: 2 Seconds      Memory Limit: 65536 KB Benny has a spacious farm land to ...

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

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

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

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

  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(并查集+位运算)

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  8. hdu 1198 Farm Irrigation(深搜dfs || 并查集)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...

  9. HDU 1198 Farm Irrigation (并查集优化,构图)

    本题和HDU畅通project类似.仅仅只是畅通project给出了数的连通关系, 而此题须要自己推断连通关系,即两个水管能否够连接到一起,也是本题的难点所在. 记录状态.不断combine(),注意 ...

随机推荐

  1. c#发展前景

    根据育龙网资料评价显示:C#几乎集中了所有关于软件开发和软件工程研究的最新成果:面向对象.类型安全.组件技术.自动内存管理.跨平台异常处理.版本控制.代码安全管理…….尽管像很多人注意到的一样,罗列上 ...

  2. cleanMyMac

    想看外国网站可以找我,facebook.youtube.XX大片等,只要8元钱,无限制用到服务器关闭.看大片流畅不成问题 需要cleanMyMac的请加微信只要10或直接拍 http://a.p6ff ...

  3. POI 导出导入工具类介绍

    介绍: Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. .NET的开发人员则可以利用NPOI (POI ...

  4. 用 HAproxy 搭建 RabbitMQ 集群

    构建参考: [ Rabbitmq cluster setup with HAproxy ] [ python demo ] RabbitMQ Cluster 遇到的问题 python pika 作为c ...

  5. Linux上安装Redis

    很多编程的小朋友一提到Linux脑袋就大了,我也一样,我是一个大专的学生,没有学过Linux,感觉自己欠缺很多,也知道了人和人之间的差距,当你真正的走上社会,才知道社会是什么,才知道没有学历找工作有多 ...

  6. 在ssh框架中service,action,jsp,formbeam,dao的调用顺序

    本文来自:http://blog.csdn.net/w_basketboy24/article/details/8642846 jsp发起请求. actionform封装请求参数. action接受请 ...

  7. TPYBoard读取芯片上的温度传感器

    转载请以链接形式注明文章来源,公众号:MicroPython玩家汇 一.STM32内部温度传感器概要 STM32芯片内部一项独特的功能就是内部集成了一个温度传感器,因为是内置,所以测试的是芯片内部的温 ...

  8. [Spark内核] 第29课:Master HA彻底解密

    本课主题 Master HA 解析 Master HA 解析源码分享 [引言部份:你希望读者看完这篇博客后有那些启发.学到什么样的知识点] 更新中...... Master HA 解析 生产环境下一般 ...

  9. 加速scp传输速度

    当需要在机器之间传输400GB文件的时候,你就会非常在意传输的速度了.默认情况下(约125MB带宽,网络延迟17ms,Intel E5-2430,本文后续讨论默认是指该环境),scp的速度约为40MB ...

  10. Linux基本操作笔记

    1.Linux是一个统称,内核是一致的.分为Linux系统管理员和Linux程序员包括管理和软件开发. 2.要掌握Linux,有四步,第一,在Linux平台上的开发,比如,vi.gcc.gdb等和Li ...