Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3451    Accepted Submission(s):
1128

Problem Description
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.
 
Input
The 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
 
Output
For 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
 
Author
 
能不能用交替进行的方式进行双向广搜?
不行,因为可能你走的可能不是通往最佳的@的。
另加一组数据

6 5
Y.@#.
.#.#.
.#.#.
.#.#.
.#.#.
##M..

77

 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std; int n,m;
int yx,yy,mx,my;
int to[][]={{,},{,},{-,},{,-}};
char a[][];
int time[][][];
bool hash[][][];
bool flag;
struct node
{
int x,y;
int time;
};
queue<node>Q[]; int Min(int x,int y)
{
return x>y? y:x;
}
bool pd(node &t)
{
if(t.x>=&&t.x<=n && t.y>=&&t.y<=m && a[t.x][t.y]!='#')return false;
return true;
}
int bfs(int x)
{
int i,hxl=;
node t,cur; while(!Q[x].empty())
{
cur=Q[x].front();
Q[x].pop();
for(i=;i<;i++)
{
t=cur;
t.x=t.x+to[i][];
t.y=t.y+to[i][];
t.time++;
if(pd(t))continue;
if(hash[x][t.x][t.y])continue;
hash[x][t.x][t.y]=true;
time[x][t.x][t.y]=t.time;
if(x== && a[t.x][t.y]=='@')
{
hxl=Min(hxl,time[x^][t.x][t.y]+time[x][t.x][t.y]);
}
Q[x].push(t);
}
}
return hxl;
}
void dbfs()
{
int ans=;
node t;
t.x=yx;
t.y=yy;
t.time=;
Q[].push(t);
hash[][yx][yy]=true; t.x=mx;
t.y=my;
t.time=;
Q[].push(t);
hash[][mx][my]=true; bfs();
ans = bfs();
printf("%d\n",ans*);
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)>)
{
for(i=;i<=n;i++)
scanf("%s",a[i]+);
memset(hash,false,sizeof(hash));
memset(time,,sizeof(time));
for(i=;i<=n;i++)
for(j=;j<=m;j++){
if(a[i][j]=='Y'){
yx=i;
yy=j;
}
else if(a[i][j]=='M'){
mx=i;
my=j;
}
}
while(!Q[].empty()){
Q[].pop();
}
while(!Q[].empty()){
Q[].pop();
}
flag=false;
dbfs();
}
return ;
}
 

hdu 2612的更多相关文章

  1. HDU 2612 Find a way(找条路)

    HDU 2612 Find a way(找条路) 00 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)   Problem  ...

  2. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

  3. BFS(最短路) HDU 2612 Find a way

    题目传送门 /* BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 */ /***************************************** ...

  4. 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 ...

  5. hdu 2612 Find a way

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Description Pass a year learning in H ...

  6. HDU 2612 - Find a way - [BFS]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Problem DescriptionPass a year learning in Hangz ...

  7. HDU 2612 Find a way bfs 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...

  8. (广搜) Find a way -- hdu -- 2612

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 MS (Java/Others) ...

  9. HDU - 2612 Find a way 【BFS】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2612 题意 有两个人 要去一个城市中的KFC 一个城市中有多个KFC 求两个人到哪一个KFC的总时间最 ...

  10. HDU 2612 (BFS搜索+多终点)

    题目链接: http://poj.org/problem?id=1947 题目大意:两人选择图中一个kfc约会.问两人到达时间之和的最小值. 解题思路: 对于一个KFC,两人的BFS目标必须一致. 于 ...

随机推荐

  1. Day 25 多态.

    一.多态的概念 多态指的是一类事物有多种形态.动物有多种形态:人,狗,猪 from abc import ABCMeta,abstractmethod class Animal(metaclass=a ...

  2. 2018-2019-2 20175306实验三敏捷开发与XP实践《Java开发环境的熟悉》实验报告

    2018-2019-2 20175306实验三敏捷开发与XP实践<Java开发环境的熟悉>实验报告 实验内容 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先 ...

  3. “全栈2019”Java多线程第二十六章:同步方法生产者与消费者线程

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  4. jzoj3363

    JYY 来到了一个新的城市,为了和大家保持联系,第一件事就是办理新的手机号.JYY 对号码的要求很高,希望大家都能够顺畅地阅读手机号,因此 JYY 特别研究了地球人的电话号码阅读习惯,有如下重大发现 ...

  5. 前端ajax传数据成功发送,但后端接收不到

    前几天遇到这样的问题,找了好久,是在ajax     contentType属性设置的问题. contentType默认是application/x-www-form-urlencoded    但是 ...

  6. `Vue`中为什么访问不了以`$`和`_`开头的属性?

    Vue中为什么访问不了以$和_开头的属性? 背景:航班管家H5使用了Vue进行新版开发,预订流程逻辑copy参考了野鹅国际机票小程序,小程序中使用__开头的属性作为私有属性. 如题,在data中定义的 ...

  7. cmd下查看应用端口情况

    在win10开始窗口右侧的空白处点击CMD,在上方弹出窗口中选择命令提示符,双击进入 在弹出命令界面中,输入netstat -na命令后回车,如下图所示,可以看到所有目前打开的端口 如果要查看打开端口 ...

  8. 【xsy1232】Magic 最小割

    题目大意:给你一个$n$个点,$m$条有向边的图,每个点有一个点权$a_i$,同时你可以用$b_i$的代价将$a_i$变为$0$ 另外你要付出$\sum\limits_{i=1}^n\max\limi ...

  9. Machine learning 第8周编程作业 K-means and PCA

    1.findClosestCentroids function idx = findClosestCentroids(X, centroids) %FINDCLOSESTCENTROIDS compu ...

  10. mysql清空表命令:delete和truncate区别

    mysql清空表可以用delete和truncate两个命令来完成: 1. delete ① 语法:delete from table_name: ② 示例:DELETE FROM `order`; ...