HDU 2412 Farm Irrigation
题目链接:
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的更多相关文章
- HDU 1198 Farm Irrigation(状态压缩+DFS)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目: Farm Irrigation Time Limit: 2000/1000 MS (Ja ...
- ZOJ 2412 Farm Irrigation
Farm Irrigation Time Limit: 2 Seconds Memory Limit: 65536 KB Benny has a spacious farm land to ...
- hdu.1198.Farm Irrigation(dfs +放大建图)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- ZOJ 2412 Farm Irrigation(DFS 条件通讯块)
意甲冠军 两个农田管内可直接连接到壳体 他们将能够共享一个水源 有11种农田 管道的位置高于一定 一个农田矩阵 问至少须要多少水源 DFS的连通块问题 两个相邻农田的管道能够直接连接的 ...
- HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1198 Farm Irrigation(并查集,自己构造连通条件或者dfs)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1198 Farm Irrigation(并查集+位运算)
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- hdu 1198 Farm Irrigation(深搜dfs || 并查集)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...
- HDU 1198 Farm Irrigation (并查集优化,构图)
本题和HDU畅通project类似.仅仅只是畅通project给出了数的连通关系, 而此题须要自己推断连通关系,即两个水管能否够连接到一起,也是本题的难点所在. 记录状态.不断combine(),注意 ...
随机推荐
- c#加密解密源码,md5、des、rsa
从网上找来的代码,顺手改改,用起来更方便. 配置文件 using System; using System.Collections.Generic; using System.Text; using ...
- RabbitMQ教程(一) ——win7下安装RabbitMQ
RabbitMQ依赖erlang,所以先安装erlang,然后再安装RabbitMQ; 下载RabbitMQ,下载地址: rabbitmq-server-3.5.6.exe和erlang,下载地址:o ...
- 通过 备份文件 恢复/迁移 gitlab
=============================================== 2017/10/20_第1次修改 ccb_warlock = ...
- Linux(CentOS6.5)修改系统市区被中国标准时间(北京时间)
本文地址http://comexchan.cnblogs.com/ ,作者Comex Chan,尊重知识产权,转载请注明出处,谢谢! 备份时区配置文件 cp /etc/localtime /etc/l ...
- [编织消息框架][JAVA核心技术]异常应用
QException是项目业务异常基类 按模块划分子类异常,方便定位那块出错 有个来源码属性code作用定位某个功能处理出错逻辑,数字类型节省内存空间,同时减少创建子类的子类 QSocketExcep ...
- Parallels Desktop 12
我微新solq123987654 备注:PD 科普:PD12有什么用,PD是让mac系统可以运行wind系统的软件,如果你不习惯mac os 或工作需要那PD绝对是个好软件正版要七八百授权,只要十五就 ...
- 在Maven Central发布中文API的Java库
原址: https://zhuanlan.zhihu.com/p/28024364 相关问题: 哪些Java库有中文命名的API? 且记下随想. 之前没有发布过, 看了SO上的推荐:Publish a ...
- Java求循环节长度
两个整数做除法,有时会产生循环小数,其循环部分称为:循环节.比如,11/13=6=>0.846153846153..... 其循环节为[846153] 共有6位.下面的方法,可以求出循环节的长 ...
- SqlServer Lock_Escalation
在今天的文章里,我想谈下SQL Server里锁升级(Lock Escalations).锁升级是SQL Server使用的优化技术,用来控制在SQL Server锁管理里把持锁的数量.我们首先用SQ ...
- java内存溢出问题
相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题,这个问题曾困扰了我很长时间,随着解决各类问题经验的积累以及对问题根源的探索,终于有了一个比较深入的认识. 在解决j ...