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. 

InputThe 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 
OutputFor 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 思路:典型的BFS,两次BFS并记录步数。
AC Code:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#include<utility>
using namespace std;
typedef pair<int,int> P;
int step[][]= {,,-,,,,,-};
bool vis[][];
char map[][];
int stepnum[][][];
int n,m,INF=0x3f3f3f3f;
struct node
{
P p;
int step;
};
int check(int x,int y)
{
if(x>=&&x<=n&&y>=&&y<=m&&map[x][y]!='#')
return ;
return ;
}
void BFS(int a,P p)
{
memset(vis,false,sizeof(vis)); vis[p.first ][p.second]=true;
node point,newpoint; queue<node> q;
point.p .first=p.first ;
point.p .second=p.second ;
point.step=;
q.push(point); while(!q.empty())
{
point=q.front();
q.pop();
if(map[point.p.first][point.p.second ]=='@')//遇到KFC 记录所走的步数
{
stepnum[a][point.p.first ][point.p.second ]=point.step;
}
for(int i=; i<; i++)
{
newpoint.p.first =point.p.first+step[i][];
newpoint.p.second =point.p.second +step[i][];
if(check(newpoint.p.first,newpoint.p.second)&&!vis[newpoint.p.first][ newpoint.p.second])
{
vis[newpoint.p.first][ newpoint.p.second]=true;
newpoint.step=point.step+;//每走一格 步数+1
stepnum[a][newpoint.p.first][newpoint.p.second]=newpoint.step;
q.push(newpoint);
}
}
}
}
int main (void)
{
while(~scanf("%d%d",&n,&m))
{
P Y,M;
getchar();
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='Y')
Y=make_pair(i,j);
else if(map[i][j]=='M')
M=make_pair(i,j);
}
getchar();
}
memset(stepnum,INF,sizeof(stepnum));//一定要放在BFS之前 如果放在BFS里面,第二次BFS时,会把第一次BFS的值覆盖 结果出错
BFS(,Y);
BFS(,M);
int step_num=INF;
for(int i=; i<=n; i++)
for(int j=; j<=m; j++)
if(map[i][j]=='@')//找出同一个KFC中需要走最小的那个点
step_num=min(step_num,stepnum[][i][j]+stepnum[][i][j]);
printf("%d\n",step_num*);
}
return ;
}

Find a way HDU - 2612的更多相关文章

  1. HDU 2612 Find a way(找条路)

    HDU 2612 Find a way(找条路) 00 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)   Problem  ...

  2. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

  3. BFS(最短路) HDU 2612 Find a way

    题目传送门 /* BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 */ /***************************************** ...

  4. HDU 2612 Find a way(双向bfs)

    题目代号:HDU 2612 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 M ...

  5. hdu 2612 Find a way

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Description Pass a year learning in H ...

  6. HDU 2612 - Find a way - [BFS]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Problem DescriptionPass a year learning in Hangz ...

  7. HDU 2612 Find a way bfs 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...

  8. (广搜) Find a way -- hdu -- 2612

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 MS (Java/Others) ...

  9. HDU - 2612 Find a way 【BFS】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2612 题意 有两个人 要去一个城市中的KFC 一个城市中有多个KFC 求两个人到哪一个KFC的总时间最 ...

  10. HDU 2612 (BFS搜索+多终点)

    题目链接: http://poj.org/problem?id=1947 题目大意:两人选择图中一个kfc约会.问两人到达时间之和的最小值. 解题思路: 对于一个KFC,两人的BFS目标必须一致. 于 ...

随机推荐

  1. SpringBoot Mybatis 分页插件PageHelper

    添加maven配置: <!-- 分布插件 --> <dependency> <groupId>com.github.pagehelper</groupId&g ...

  2. HDU 4825 Xor Sum(01字典树)题解

    思路:先把所有数字存进字典树,然后从最高位贪心. 代码: #include<set> #include<map> #include<stack> #include& ...

  3. FileAttributes Enum

    https://docs.microsoft.com/en-us/dotnet/api/system.io.fileattributes?view=netframework-4.7.2 读取FileA ...

  4. ssm项目部署到服务器过程

    ssm项目部署到服务器过程 特别篇 由于准备春招,所以希望各位看客方便的话,能去github上面帮我Star一下项目 https://github.com/Draymonders/Campus-Sho ...

  5. SQL Server (MSSQLSERVER) 服务因 2148081668 服务性错误而停止。

    https://zhidao.baidu.com/question/151448005.html 具体步骤: 运行-> 输入:“services.msc” ->找到 “SQL Server ...

  6. HDU 1465 不容易系列之一

    扯淡 貌似有傻逼的做法XD 话说我没开long long,忘读入n,忘了清零ans WA了三遍是什么操作啊 傻了傻了 思路 显然是一个错排问题啊XD 但是我们不套公式,我们用一发二项式反演 二项式反演 ...

  7. vue 弹性布局 实现长图垂直居上,短图垂直居中

    vue 弹性布局 实现长图垂直居上,短图垂直居中 大致效果如下图,只考虑垂直方向.长图可以通过滚动条看,短图居中效果,布局合理 html代码(vue作用域内): <div class=" ...

  8. 用Qemu模拟vexpress-a9 --- 配置 qemu 的网络功能

    转载:http://wiki.sylixos.com/index.php/Linux%E7%8E%AF%E5%A2%83%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97 环境介 ...

  9. PTA 7-1 整数分解为若干项之和(20 分)

    7-1 整数分解为若干项之和(20 分) 将一个正整数N分解成几个正整数相加,可以有多种分解方法,例如7=6+1,7=5+2,7=5+1+1,….编程求出正整数N的所有整数分解式子. 输入格式: 每个 ...

  10. 简易Samba服务器配置

    Samba的作用是在Linux和windows之间通过网络进行资源共享.下面是简单的一个文件共享例子: 1.安装samba.samba-client服务 yum install samba samba ...