题目链接:

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. 浅析Node.js的Event Loop

    目录 浅析Node.js的Event Loop 引出问题 Node.js的基本架构 Libuv Event Loop Event Loop Phases Overview Poll Phase The ...

  2. readAsDataURL(file) & readAsText(file, encoding)

      readAsDataURL(file)会把文件内容转换为data类型的URL: data:text/plain;base64,b3JkZXItaWQJb3JkZXItaXRlbS1p... 这种d ...

  3. Android开发之漫漫长途 IX——彻底掌握Binder

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  4. vue移动端弹框组件,vue-layer-mobile

    最近做一个移动端项目,弹框写的比较麻烦,查找资料,找到了这个组件,但是说明文档比较少,自己研究了下,把我碰到的错,和详细用法分享给大家!有疑问可以打开组件看一看,这个组件是仿layer-mobile的 ...

  5. php array_walk

    PHP array_walk() 函数 对数组中的每个元素应用用户自定义函数: <?php function myfunction($value,$key) { echo "The k ...

  6. C#用DataTable实现Group by数据统计

    http://www.cnblogs.com/sydeveloper/archive/2013/03/29/2988669.html 1.用两层循环计算,前提条件是数据已经按分组的列排好序的. Dat ...

  7. nvm 装 nodejs 重启终端失效的解决方法

    (1) 安装 nvm wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash (2) ...

  8. C# 的基本数据类型

    bool System.Boolean 4Byte 32bit布尔型变量 逻辑值,true或者false,默认值为false byte System.Byte 1Byte 8bit无符号整数无符号的字 ...

  9. C#中yield的使用

    yield 关键字向编译器指示它所在的方法是迭代器块.编译器生成一个类来实现迭代器块中表示的行为.在迭代器块中,yield 关键字与 return 关键字结合使用,向枚举器对象提供值.这是一个返回值, ...

  10. PE文件简介

    PE文件的全称是Portable Executable,意为可移植的可执行的文件,常见的EXE.DLL.OCX.SYS.COM都是PE文件,PE文件是微软Windows操作系统上的程序文件(可能是间接 ...