HDU/HDOJ 2612 Find a way 双向BFS
题目链接: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的更多相关文章
- 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 ...
- hdoj 2612 Find a way【bfs+队列】
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 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 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- 2017多校第10场 HDU 6171 Admiral 双向BFS或者A*搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6171 题意: 给你一个高度为6的塔形数组,你每次只能将0与他上下相邻的某个数交换,问最少交换多少次可以 ...
- HDU 1043 Eight(双向BFS+康托展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- HDU 3085 Nightmare Ⅱ(双向BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
随机推荐
- Android 代码混淆
什么是代码混淆 Java 是一种跨平台的.解释型语言,Java 源代码编译成中间”字节码”存储于 class 文件中.由于跨平台的需要,Java 字节码中包括了很多源代码信息,如变量名.方法名,并且通 ...
- FileStream -- 复制文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 编译kernel:编译
韦东山Linux视频第1期_裸板_UBoot_文件系统_驱动初步第10课第3节 内核启动流程分析之Makefile.WMV 1. 编译内核分三步: make xxx_defconfig [linux ...
- 利用Ihttpmodel实现网站缓存,解决Server.Transfer 直接输出HTML源代码的问题
今天在用.NET利用IHttpModel实现网站静态缓存的时候,不知道最后为什么用 Server.Transfer(html)的时候结果输出的是HTML的源代码. 贴上源代码 using System ...
- Linux服务器使用SSH的命令(有详细的参数解释)
前一阵远程维护Linux服务器,使用的是SSH,传说中的secure shell. 登陆:ssh [hostname] -u user 输入密码:***** 登陆以后就可以像控制自己的机器一样控制它了 ...
- android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 3
今天在写一个小项目的数据库部分的功能时,出现了一个这样的问题:java.lang.RuntimeException: Failure delivering result ResultIn ...
- CentOS 配置 ssh
默认安装ssh是有的.只是hosts访问问题. 1.在hosts.deny文件尾添加sshd:ALL意思是拒绝所有访问请求 [root@localhost ~]# vi /etc/hosts.de ...
- IT段子,娱乐一下
1.我是个程序员,一天我坐在路边一边喝水一边苦苦检查bug.这时一个乞丐在我边上坐下了,开始要饭,我觉得可怜,就给了他1块钱,然后接着调试程序.他可能生意不好,就无聊的看看我在干什么,然后过了一会,他 ...
- TinkPHP+WAMP
1.下载wamp 我下载的是php5.5版本:根据你自身的需要嘛 http://www.wampserver.com/ 2.下载thinkphp 我下载的版本是3.2 http://www.think ...
- Swift - 给游戏添加背景音乐和音效(SpriteKit游戏开发)
游戏少不了背景音乐和音效.下面我们通过创建一个管理音效的类,来实现背景音乐的播放,同时点击屏幕可以播放相应的音效. 声音管理类 SoundManager.swift 1 2 3 4 5 6 7 8 9 ...