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. Mac OS 10.12 - 安装Homebrew,像Ubuntu里面的apt一样简单地安装和删除软件!

    Homebrew — macOS 不可或缺的套件管理器,Homebrew官方网站如此介绍自己!!! 中文官网:https://brew.sh/index_zh-cn.html 一,安装 打开shell ...

  2. 782. Transform to Chessboard

    An N x N board contains only 0s and 1s. In each move, you can swap any 2 rows with each other, or an ...

  3. jQuery基础笔记(4)

    day55 参考:https://www.cnblogs.com/liwenzhou/p/8178806.html#autoid-1-9-3 文本操作 HTML代码: html()// 取得第一个匹配 ...

  4. php中strpos()函数

    1,strpos()函数 mixed strops(]) 返回needle在haystack中首次出现的数字位置,从0开始查找,区分大小写. 参数:haystack,在该字符串中进行查找. needl ...

  5. iOS多线程---NSOperation介绍和使用

    1.  NSOperation实现多线程编程,需要和NSOperationQueue一起使用. (1)先将要执行的操作封装到NSOperation中 (2)将NSOperation对象添加到NSOpe ...

  6. git命令上传项目到码云总结

    码云上传项目git命令总结: git clone https://git.oschina.net/xh-lxx/xh-lxx.oschina.io.git 进入到克隆下来的文件夹,然后操作git命令 ...

  7. Web应用三种部署方式的优缺点

    方式一:修改server.xml文件 优点: 配置速度快,只需要在server.xml文件中添加<Context>标签,在其中分别配置path虚拟路径和docBase真实路径然后启动Tom ...

  8. 使用findbugs为自己的代码review

    转自:http://blog.lichengwu.cn/java/2013/11/24/use-findbugs-code-review/ 介绍 Findbugs是一个代码静态分析工具,用来找出Jav ...

  9. Java多线程——死锁

    当一个线程永远地持有一个锁,并且其他线程都尝试获得这个锁时,那么他永远被阻塞,当线程A持有锁L并想获得锁M的同时,线程B持有锁M并同时尝试获得锁L时,那么两个线程将永远的等待下去,这中情况就是简单的死 ...

  10. Binder AIDL中自定义类型传递的源码分析

    binder机制实现的IPC和共享内存的方式不同,它采取的是值拷贝的方式,即进程间传递的实体遵循Parcelable协议, Bp端负责向Parcel里写东西,Bn端负责从Parcel里读取还原,顺序是 ...