博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu2612——Find a way(BFS)
阅读量:2345 次
发布时间:2019-05-10

本文共 1912 字,大约阅读时间需要 6 分钟。

Problem Description

Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.

Input

The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF

Output

For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.

Sample Input

4 4
Y.#@
….
.#..
@..M
4 4
Y.#@
….
.#..
@#.M
5 5
Y..@.
.#…
.#…
@..M.
#…#

Sample Output

66
88
66

两个人准备在某个KFC见面,由于时间是算两个人一共的和,所以要做两次BFS,计算每个人到某个KFC的最短时间,然后加起来比较最短的那个。

坑点是有的KFC可能有人到不了,所以要做好标记。。。

#include 
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 100010using namespace std;char mp[205][205];int dx[]= { 0,0,1,-1};int dy[]= { 1,-1,0,0};int n,m,visa[205][205],visb[205][205];struct Node{ int x,y,step;};map
, int > h;void bfs(Node a,int vis[][205]){ vis[a.x][a.y]=1; queue
q; q.push(a); while(!q.empty()) { Node tmp=q.front(); q.pop(); //cout<
<<" "<
<<" "<
<
=0&&tmp1.x
=0&&tmp1.y
>mp[i][j]; if(mp[i][j]=='Y') { a.x=i; a.y=j; a.step=0; } if(mp[i][j]=='M') { b.x=i; b.y=j; b.step=0; } if(mp[i][j]=='@') { h[make_pair(i,j)]=0; } } bfs(a,visa); bfs(b,visb); int ans=INF; for(int i=0; i

转载地址:http://picvb.baihongyu.com/

你可能感兴趣的文章
有36辆自动赛车和6条跑道,没有计时器的前提下,最少用几次比赛可以筛选出最快的三辆赛车?----腾讯2016研发工程师在线模拟笔试题
查看>>
下列哪些http方法对于服务端和用户端一定是安全的?----腾讯2016研发工程师在线模拟笔试题
查看>>
对于定义"int *p",下列哪些说明可能是正确的?----腾讯2016研发工程师在线模拟笔试题
查看>>
华为研发工程师编程题----明明的随机数(快排)
查看>>
华为研发工程师编程题----进制转换(pow函数,string.find())
查看>>
华为研发工程师编程题----汽水瓶
查看>>
以下不属于tcp连接断开的状态是?----腾讯2016研发工程师笔试题
查看>>
下面关于ICMP协议的描述中,正确的是()----腾讯2016研发工程师笔试题
查看>>
对于移动平均算法,是计算某变量之前n个数值的算术平均,正确的说法是:----腾讯2016研发工程师在线模拟笔试题
查看>>
某一速率为100M的交换机有20个端口,其一个端口上连着一台笔记本电脑,此电脑从迅雷上下载一部1G的电影需要的时间可能是多久?
查看>>
在linux编程中,以下哪个TCP的套接字选项与nagle算法的开启和关闭有关?----腾讯2016研发工程师在线模拟笔试题
查看>>
某二叉树的先根遍历序列和后根遍历序列正好相反,则该二叉树具有的特征是()----腾讯2016研发工程师在线模拟笔试题
查看>>
win32系统里,下面几个sizeof的运行结果是()----腾讯2016研发工程师在线模拟笔试题
查看>>
若系统中有五台打印机,有多个进程均需要使用两台,规定每个进程一次仅允许申请一台,则在不发生死锁的情况下至多允许______个进程参与竞争
查看>>
关于红黑树和AVL树,以下哪种说法不正确?----腾讯2016研发工程师在线模拟笔试题
查看>>
关于epoll和select的区别,哪些说法是正确的?----腾讯2016研发工程师在线模拟笔试题
查看>>
以下是C++的不同数据类型值的比较语句,请问这些判断语句中作为条件部分的语句编写有问题的有:
查看>>
TCP链接中主动断开链接netstat观察可能出现的状态流转是:----腾讯2016研发工程师在线模拟笔试题
查看>>
以下涉及到内存管理的代码段中,有错误的是:----腾讯2016研发工程师在线模拟笔试题
查看>>
下面哪些特性可能导致代码体积膨胀:----腾讯2016研发工程师在线模拟笔试题
查看>>