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与他上下相邻的某个数交换,问最少交换多少次可以 ...
随机推荐
- "如何用70行Java代码实现深度神经网络算法" 的delphi版本
http://blog.csdn.net/hustjoyboy/article/details/50721535 "如何用70行Java代码实现深度神经网络算法" 的delphi ...
- 面试题:Nginx 是如何实现高并发?常见的优化手段有哪些?
面试题: Nginx 是如何实现并发的?为什么 Nginx 不使用多线程?Nginx常见的优化手段有哪些?502错误可能原因有哪些? 面试官心理分析 主要是看应聘人员的对NGINX的基本原理是否熟悉, ...
- js记住密码
$(function () { if (getCookie("rmbUser") == "true") { $("#xuanzong") ...
- Java并发AtomicBoolean类~
java.util.concurrent.atomic.AtomicBoolean类提供了可以原子读取和写入的底层布尔值的操作,并且还包含高级原子操作. AtomicBoolean支持基础布尔变量上的 ...
- Cocos2d-x之物理引擎
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 在很多的游戏设计中一般都会涉及和模拟到真实的物理世界.然而游戏中模拟真实世界的物理会很复杂.使用已经写好的物理引擎会用很大的帮助和便利. ...
- Python 学习笔记14 类 - 使用类和实例
当我们熟悉和掌握了怎么样创建类和实例以后,我们编程中的大多数工作都讲关注在类的简历和实例对象使用,修改和维护上. 结合实例我们来进一步的学习类和实例的使用: 我们新建一个汽车的类: #-*- codi ...
- Python之数字的格式化输出
需求: 将数字格式化后输出,并控制数字的位数.对齐.千位分隔符和其他的细节 x = 1234.56789 # Two decimal places of accuracy print(format(x ...
- go语言从例子开始之Example6.if/else
if 和 else 分支结构在 Go 中当然是直接了当的了. package main import "fmt" func main() { 这里是一个基本的例子. if 7%2 ...
- PL/SQL to update all columns
undefine schema_name; declare l_Err ); begin for r in (select atc.table_name, atc.column_name, atc.d ...
- Hdu-3333 Turning Tree (离线树状数组/线段树)
Hdu-3333 Turning Tree 题目大意:先给出n个数字.面对q个询问区间,输出这个区间不同数的和. 题解:这道题有多重解法.我另一篇博客写了分块的解法 HDU-3333 Turing ...