Find a way HDU - 2612
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的更多相关文章
- HDU 2612 Find a way(找条路)
HDU 2612 Find a way(找条路) 00 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- BFS(最短路) HDU 2612 Find a way
题目传送门 /* BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 */ /***************************************** ...
- 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 ...
- hdu 2612 Find a way
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Description Pass a year learning in H ...
- HDU 2612 - Find a way - [BFS]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Problem DescriptionPass a year learning in Hangz ...
- HDU 2612 Find a way bfs 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...
- (广搜) Find a way -- hdu -- 2612
链接: http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 MS (Java/Others) ...
- HDU - 2612 Find a way 【BFS】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2612 题意 有两个人 要去一个城市中的KFC 一个城市中有多个KFC 求两个人到哪一个KFC的总时间最 ...
- HDU 2612 (BFS搜索+多终点)
题目链接: http://poj.org/problem?id=1947 题目大意:两人选择图中一个kfc约会.问两人到达时间之和的最小值. 解题思路: 对于一个KFC,两人的BFS目标必须一致. 于 ...
随机推荐
- CF1131D tarjan,拓扑
题目链接 541div2 http://codeforces.com/contest/1131/problem/D 思路 给出n序列和m序列的相对大小关系 构造出最大值最小的序列 缩点+拓扑 小的向大 ...
- ActiveMQ安装使用
入门: https://www.cnblogs.com/cyfonly/p/6380860.html http://www.uml.org.cn/zjjs/201802111.asp https:// ...
- 【安装】Redis4.0.10在Linux环境搭建
1.下载Redis后上传到指定目录 2.解压 tar -zxvf redis-4.0.10.tar.gz 3.进入加压后的目录并编译 cd redis-4.0.10 make 4.进入src目录安装 ...
- nginx 配置 https 请求
1,先去这个网站申请一下证书 https://certmall.trustauth.cn/Home/Member/index/id/1521167511.html 上面会教你怎么去做. 2,就是配置自 ...
- Linux 压缩、解压命令使用
tar在Linux上是常用的打包.压缩.加压缩工具,他的参数很多,这里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数:(个人理解也就是打包) -x : 解压缩压缩档案 ...
- SQL四大语句、四大完整性、五大约束
四大语句: 1.数据定义语句: DDL:create.alter.drop.truncate(表结构) 2.数据操纵语句: DML:insert.delete.update.select 3.数据控制 ...
- 洛谷P2637第一次,第二次,成交! 模拟?DP?
今天水来一天,就贴道水吧.. 原题>>https://www.luogu.org/problem/show?pid=2637<< 题目描述 因为奶牛们的节食运动(奶牛还节食?) ...
- 转载:mysql存储过程讲解
记录MYSQL存储过程中的关键语法: DELIMITER // 声明语句结束符,用于区分; CEATE PROCEDURE demo_in_parameter(IN p_in int) 声明存储过程 ...
- rostopic demo
发布者 #!/usr/bin/env python2. # -*- coding: utf- -*- import rospy from hdw_driver.msg import update_fi ...
- 自动化测试基础-断言(Assert)使用方法
junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类.1.assertTrue/False([String message,]boolean condi ...