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

题目大意:

有两个人(用Y和M表示)要到同一个KFC(用@表示),且存在多个KFC,找一个KFC使得两人到改KFC的总时间最短,输出该最短时间。

思路:

用两次bfs分别求出Y和M到各个KFC的最短时间,可以开一个数组储存每个KFC的时间,最后相加两者的时间并取最小值。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define MAX 0x3f3f3f3f
using namespace std;
char map[][]; //用来储存原始地图
struct node
{
int x;
int y;
int step;
};
int n, m;
int T[][]; //记录时间
int nextd[][] = { ,,,-,,,-, }; //4个运动方向
void bfs(int x, int y)
{
bool vis[][]; //记录是否走过该点,true为走过,false为未经过。
memset(vis, false, sizeof(vis));
queue<node>q; //常规的bfs
node t, s;
vis[x][y] = true;
s.x = x;
s.y = y;
s.step = ;
q.push(s);
while (!q.empty())
{
s = q.front();
q.pop();
if (map[s.x][s.y] == '@')
{
if (T[s.x][s.y] == MAX) //判断Y是否已经到改@
T[s.x][s.y] = s.step;
else
T[s.x][s.y] += s.step;
}
for (int i = ; i < ; i++)
{
t.x = s.x + nextd[i][];
t.y = s.y + nextd[i][];
t.step = s.step + ;
if (t.x < || t.x >= n || t.y < || t.y >= m) continue; //出界
if (vis[t.x][t.y]) continue; //到过该点
if (map[t.x][t.y] == '#') continue; //不可经过的点
vis[t.x][t.y] = true;
q.push(t);
}
}
} int main()
{
node M, Y;
int i, j;
while (~scanf("%d %d", &n, &m))
{
memset(T, 0x3f, sizeof(T));
for (i = ; i < n; i++) scanf("%s", map[i]);
for (i = ; i < n; i++)
{
for (j = ; j < m; j++)
{
if (map[i][j] == 'M')
{
M.x = i;
M.y = j;
}
if (map[i][j] == 'Y')
{
Y.x = i;
Y.y = j;
}
}
}
/*for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d ",T[i][j]);
}printf("\n");
} */
bfs(Y.x, Y.y);
/*for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d ",T[i][j]);
}printf("\n");
} */
bfs(M.x, M.y);
/*for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d ",T[i][j]);
}printf("\n");
} */
int min = MAX;
for (i = ; i < n; i++) //找到最小的总时间
{
for (j = ; j < m; j++)
{
if (min >T[i][j])
min = T[i][j];
}
}
printf("%d\n", min*);
}
return ;
}

不给看

  

Find a Way (双bfs)的更多相关文章

  1. 2013年第四届蓝桥杯国赛 九宫重排(HashMap+双BFS优化)

    九宫重排     时间限制:1.0s   内存限制:256.0MB 问题描述 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可以移动到空格中.经过若干 ...

  2. hdu_1254_推箱子(双BFS)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1254 题解:以箱子为主体,第一层BFS,然后用第二层BFS来判断人是否可以到达,这里细节比较多,要注意 ...

  3. HDU 2612 find a way 【双BFS】

    <题目链接> 题目大意:两个人分别从地图中的Y 和 M出发,要共同在 @ 处会面(@不止有一处),问这两个人所走距离和的最小值是多少. 解题分析: 就是对这两个点分别进行一次BFS,求出它 ...

  4. UVA 11624-Fire!【双BFS】

    <题目链接> 题目大意: 你的任务是帮助J走出一个大火蔓延的迷宫.J每分钟可以超上下左右四个方向移动,而所有着火的格子每一分钟都会往四个方向蔓延一格.迷宫中有一些障碍,J和火都无法进入.当 ...

  5. Fire! (双bfs+预处理)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. 题解报告:hdu 2612 Find a way(双bfs)

    Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...

  7. poj 1475 Pushing Boxes 推箱子(双bfs)

    题目链接:http://poj.org/problem?id=1475 一组测试数据: 7 3 ### .T. .S. #B# ... ... ... 结果: //解题思路:先判断盒子的四周是不是有空 ...

  8. 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS

    [Usaco2005 Dec]Knights of Ni 骑士 Description  贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...

  9. 2017多校第10场 HDU 6171 Admiral 双向BFS或者A*搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6171 题意: 给你一个高度为6的塔形数组,你每次只能将0与他上下相邻的某个数交换,问最少交换多少次可以 ...

随机推荐

  1. C# DotNetZip压缩单、多文件以及文件夹

    有些项目为了更好的用户体验,会把下载文件做成一个压缩的文件,直接下载,免得去一个个的点击下载文件.网上有很多压缩文件的方法,也有第三方的分装DLL文件,本文主要介绍DotNetZip压缩方法. Dot ...

  2. Java + selenium 元素定位(5)之By Xpath

    这篇关于Xpath方法的文章和之前那篇CSS的方法一样,使用前,需要先掌握一些Xpath的相关知识.当然,网上也有各种工具可以帮助我们获取到元素的Xpath,但是这并不代表着我们就可以不用了解Xpat ...

  3. PAT 2019-3 7-4 Structure of a Binary Tree

    Description: Suppose that all the keys in a binary tree are distinct positive integers. Given the po ...

  4. redux请求数据流程

    redux请求数据流程 store里面的index.js文件 import {createStore,combineReducers,applyMiddleware} from "redux ...

  5. C++中的异常处理(下)

    1,catch 语句块中可以抛出异常: 1,示意图: 2,func() 在 try 语句块中,说明它有可能抛出异常,抛出的异常有可能是整型或其它类型: 3,catch 语句块处理方式是将异常重新抛出去 ...

  6. npm安装报错:Error: EACCES: permission denied

    报错如下: sudo npm i webpack -g /Users/xesfe/.npm-global/bin/webpack -> /Users/xesfe/.npm-global/lib/ ...

  7. 59.Target Sum(目标和)

    Level:   Medium 题目描述: You are given a list of non-negative integers, a1, a2, ..., an, and a target, ...

  8. python基础篇(文件操作)

    Python基础篇(文件操作) 一.初始文件操作 使用python来读写文件是非常简单的操作. 我们使用open()函数来打开一个文件, 获取到文件句柄. 然后通过文件句柄就可以进行各种各样的操作了. ...

  9. db2中将表中的某一字段设置为自增长

    DB2可以使用generated always as identity 将某一个字段指定为自增长的字段: 这表示id自动是一个自增长的字段,它从1开始增加每次增加1.也可以通过generated 将字 ...

  10. python杂货

    三.字典的基本操作 1.如何访问字典中的值? adict[key] 形式返回键key对应的值value,如果key不在字典中会引发一个KeyError. adict.get(key, default ...