题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612

思路:从两个起点出发,有多个终点,求从两个起点同时能到达的终点具有的最小时间,开两个数组分别保存两个起点到达每一个终点的用时,最后将两个

数组里的时间加起来求最小的一组,必须对应相加,因为终点必须同时到达。

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <fstream>
#include <queue>
using namespace std;
struct node{
int x,y,step;
}a[1010];
node p,q;
int n,m,sx1,sy1,sx2,sy2,ans1[1010],ans2[1010],ans[1010];
int mmin,cnt;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
char maze[205][205];
bool visit[205][205];
int judge(int x,int y){
for(int i=1;i<cnt;i++)
{
if(x==a[i].x&&y==a[i].y)return i;
}
return 0;
}
void bfs1(int x,int y){
memset(ans,0,sizeof(ans));
memset(ans1,-1,sizeof(ans1));
memset(visit,0,sizeof(visit));
queue<node> Q;
p.x=x;
p.y=y;
p.step=0;
Q.push(p);
visit[p.x][p.y]=1;
while(!Q.empty())
{
p=Q.front();
Q.pop();
int num=judge(p.x,p.y);
if(num){
ans[num]=p.step;
visit[p.x][p.y]=1;
}
for(int i=0;i<4;i++)
{
q.x=p.x+dir[i][0];
q.y=p.y+dir[i][1];
q.step=p.step+1;
if(q.x<0||q.x>=n||q.y<0||q.y>=m)continue;
if(visit[q.x][q.y])continue;
if(maze[q.x][q.y]=='#')continue;
Q.push(q);
visit[q.x][q.y]=1;
}
}
for(int i=1;i<cnt;i++){
if(ans[i])ans1[i]=ans[i];
} }
void bfs2(int x,int y){
memset(ans,0,sizeof(ans));
memset(ans2,-1,sizeof(ans2));
memset(visit,0,sizeof(visit));
queue<node> Q;
p.x=x;
p.y=y;
p.step=0;
Q.push(p);
visit[p.x][p.y]=1;
while(!Q.empty())
{
p=Q.front();
Q.pop();
int num=judge(p.x,p.y);
if(num){
ans[num]=p.step;
visit[p.x][p.y]=1;
}
for(int i=0;i<4;i++)
{
q.x=p.x+dir[i][0];
q.y=p.y+dir[i][1];
q.step=p.step+1;
if(q.x<0||q.x>=n||q.y<0||q.y>=m)continue;
if(visit[q.x][q.y])continue;
if(maze[q.x][q.y]=='#')continue;
Q.push(q);
visit[q.x][q.y]=1;
}
}
for(int i=1;i<cnt;i++){
if(ans[i])ans2[i]=ans[i];
}
}
int main()
{
//ifstream fin;
//fin.open("data1.txt"); while(cin>>n>>m)
{
cnt=1;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
cin>>maze[i][j];
if(maze[i][j]=='Y'){
sx1=i;
sy1=j;
}
if(maze[i][j]=='M'){
sx2=i;
sy2=j;
}
if(maze[i][j]=='@'){
a[cnt].x=i;
a[cnt++].y=j;
}
}
mmin=999999;
bfs1(sx1,sy1);
bfs2(sx2,sy2);
for(int i=1;i<cnt;i++)
{
if(ans1[i]!=-1&&ans2[i]!=-1){
int tsum=ans1[i]+ans2[i];
if(mmin>tsum)mmin=tsum;
}
}
cout<<mmin*11<<endl;
}
return 0; }

HDU/HDOJ 2612 Find a way 双向BFS的更多相关文章

  1. Eight (HDU - 1043|POJ - 1077)(A* | 双向bfs+康拓展开)

    The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...

  2. hdoj 2612 Find a way【bfs+队列】

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

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

  4. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  5. 2017多校第10场 HDU 6171 Admiral 双向BFS或者A*搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6171 题意: 给你一个高度为6的塔形数组,你每次只能将0与他上下相邻的某个数交换,问最少交换多少次可以 ...

  6. HDU 1043 Eight(双向BFS+康托展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...

  7. HDU.2612 Find a way (BFS)

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

  8. HDU 3085 Nightmare Ⅱ(双向BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...

  9. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

随机推荐

  1. Android Drawable 与 LayerList综合汇总

    先看需求.要求这样的效果 上代码 <?xml version="1.0" encoding="utf-8"? > <layer-list xm ...

  2. 关于SSH框架设计的一些理解

    近期在学习企业开发领域非常流行的SSH框架(Struts.Hibernate.Spring).因为之前有做过原生的Servlet+JSP的项目,所以在学习过程中我会跟原生开发模式进行对照,在这里我把自 ...

  3. c语言:链表排序, 链表反转

    下面将实现链表排序的排序和遍历显示功能: 所定义的链表结构如下: head -> p1 -> p2 ->p3 ->....->pn; head的本身不作为数据节点,hea ...

  4. javascript每日一练(三)——DOM一

    一.Dom基础 childNodes(有兼容问题),children nodeType getAttribute() firstChild,lastChild,previousSilbing,next ...

  5. JAVA UTF-8字符转换为GBK

    String t = "\u53d6"; try { String gbk=URLEncoder.encode(t,"GBK"); System.out.pri ...

  6. JS实现图片翻书效果

    picture.html <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http- ...

  7. [WCF]WCF起航

    解决方案概览: Client:windows 控制台应用程序. WcfService1: windows 服务应用程序. WCFWebTest:asp.net 空web应用程序. 变量程序命名.结构可 ...

  8. POJ 2635 The Embarrassed Cryptographer 高精度

    题目地址: http://poj.org/problem?id=2635 题意:给出一个n和L,一直n一定可以分解成两个素数相乘. 让你判断,如果这两个素数都大于等于L,则输出GOOD,否则输出最小的 ...

  9. Eclipse更改默认工作目录的方法

    参考: Eclipse更改默认工作目录的方法:http://blog.163.com/take_make/blog/static/208212210201272611406227/ 用记事本打开&qu ...

  10. Round Numbers

    转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1301472836 大致题意: 输入两个十进制正整数a和b,求闭区间 [a ,b] 内有多少 ...