Find a Way (双bfs)
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)的更多相关文章
- 2013年第四届蓝桥杯国赛 九宫重排(HashMap+双BFS优化)
九宫重排 时间限制:1.0s 内存限制:256.0MB 问题描述 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可以移动到空格中.经过若干 ...
- hdu_1254_推箱子(双BFS)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1254 题解:以箱子为主体,第一层BFS,然后用第二层BFS来判断人是否可以到达,这里细节比较多,要注意 ...
- HDU 2612 find a way 【双BFS】
<题目链接> 题目大意:两个人分别从地图中的Y 和 M出发,要共同在 @ 处会面(@不止有一处),问这两个人所走距离和的最小值是多少. 解题分析: 就是对这两个点分别进行一次BFS,求出它 ...
- UVA 11624-Fire!【双BFS】
<题目链接> 题目大意: 你的任务是帮助J走出一个大火蔓延的迷宫.J每分钟可以超上下左右四个方向移动,而所有着火的格子每一分钟都会往四个方向蔓延一格.迷宫中有一些障碍,J和火都无法进入.当 ...
- Fire! (双bfs+预处理)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- 题解报告:hdu 2612 Find a way(双bfs)
Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...
- poj 1475 Pushing Boxes 推箱子(双bfs)
题目链接:http://poj.org/problem?id=1475 一组测试数据: 7 3 ### .T. .S. #B# ... ... ... 结果: //解题思路:先判断盒子的四周是不是有空 ...
- 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS
[Usaco2005 Dec]Knights of Ni 骑士 Description 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...
- 2017多校第10场 HDU 6171 Admiral 双向BFS或者A*搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6171 题意: 给你一个高度为6的塔形数组,你每次只能将0与他上下相邻的某个数交换,问最少交换多少次可以 ...
随机推荐
- Selenium:火狐Try Xpath插件替代Firebug和Firepath
什么是Xpath? XPath是XML的路径语言,通俗一点讲就是通过元素的路径来查找到这个标签元素. 工具 Xpath的练习建议大家安装火狐浏览器后,下载插件,try path. 在Selenium中 ...
- vue中修改了数据但视图无法更新的情况(转)
原文地址:https://blog.csdn.net/qq_39985511/article/details/79778806
- python 装饰器 对类和函数的装饰
#装饰器:对类或者函数进行功能的扩展 很多需要缩进的没有进行缩进'''#第一步:基本函数def laxi(): print('拉屎')#调用函数laxi()laxi() print('======= ...
- 第6章 RPC之道
6.1 认识RPC 分布式.微服务的架构思维中都不能缺少 RPC 的影子 RPC(Remote Procedure Call)远程过程调用.通过网络在跨进程的两台服务器之间传输信息,我们使用的时候不用 ...
- Hadoop2.2.0在Ubuntu编译失败解决方法
[INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE ...
- unique(去重函数)
去重排序(unique函数的使用) 2013年05月30日 11:05:45 阅读数:9689更多 个人分类: 字符串处理 出处:http://www.cnblogs.com/QQbai/archi ...
- bzoj2521 [Shoi2010]最小生成树
[Shoi2010]最小生成树 Time Limit: 10 Sec Memory Limit: 128 MB Description Secsa最近对最小生成树问题特别感兴趣.他已经知道如果要去求出 ...
- 获取模糊匹配的div id属性
html中有一批id,以数字+固定字符结尾,前台需要把这一批id组成数组传递给后台 假设固定结尾字符为“pic”,使用 var pidlist=$("[id$='pic']");将 ...
- 2019-9-2-win10-uwp-切换主题
title author date CreateTime categories win10 uwp 切换主题 lindexi 2019-09-02 12:57:38 +0800 2018-2-13 1 ...
- python实现WordCount(第三次作业)
0x00 注明 合作者:201631062315 201631062310 代码地址:https://gitee.com/c1e4r/word-count2 作业地址:https://edu.cnbl ...