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与他上下相邻的某个数交换,问最少交换多少次可以 ...
随机推荐
- 解决让刷新页面时不提示 "重试或取消”对话框
如果刷新一个已经提交过的页面时,系统总是会提示一个 "重试或取消”的对话框.,如果是一个普通的页面,好象也无所谓,有就有,大不了多点一下.但是当我们是在子窗体中刷新父窗体时,就显得有点多余了 ...
- T1218:取石子游戏
[题目描述] 有两堆石子,两个人轮流去取.每次取的时候,只能从较多的那堆石子里取,并且取的数目必须是较少的那堆石子数目的整数倍,最后谁能够把一堆石子取空谁就算赢. 比如初始的时候两堆石子的数目是25和 ...
- mybatis分页插件使用
一:导入依赖 <!--分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> < ...
- Scrapy框架: 通用爬虫之CSVFeedSpider
步骤01: 创建项目 scrapy startproject csvfeedspider 步骤02: 使用csvfeed模版 scrapy genspider -t csvfeed csvdata g ...
- 公司redis
一: redis cluster介绍篇 1:redis cluster的现状 目前redis支持的cluster特性(已亲测): 1):节点自动发现 2):slave->master 选举,集群 ...
- vue 纯前端导出 excel 表格
在开发后台管理系统的时候,很多地方都要用到导出excel 表格,比如将table中的数据导出到本地,那么实现这种需求往往有两种方案: 一.后端开发一个下载链接,前端将这个链接放到 a 标签的 href ...
- 轻松上手nodeJs爬取想要页面的数据
开始之前请先确保自己安装了Node.js环境!!!!!!!! 1.在项目文件夹安装两个必须的依赖包 npm install superagent -S SuperAgent(官网是这样解释的) --- ...
- 后台执行循环(done &)
- linux 系统磁盘管理体系
目录 linux 系统磁盘管理体系 一.磁盘的基本概念 二.磁盘的内部结构 三.磁盘的外部结构 四.磁盘的接口及类型 五.fdisk磁盘分区实践 六.gdisk 分区 七.parted 高级分区工具. ...
- k8s--网络模式
1.clusterip kind: Service apiVersion: v1 metadata: name: my-service spec: selector: app: nginx ports ...