题目链接:Rescue

进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢。。。为什么不试着改变一下捏。。。。

開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出事。。。后来发现题目里面也有坑

题意是从r到a的最短距离,“.”相当时间单位1,“x”相当时间单位2,求最短时间

HDU 搜索课件上说,这题和HDU1010相似,刚開始并没有认为像剪枝,就改用  双向BFS   0ms  一Y,爽!

网上查了一下,神牛们居然用BFS+优先队列。。。顿悟

那么本题的搜索树能够理解为root 为a,连接若干枝条,枝条的叶子就是r,那么深度大的枝条剪枝,深度最小的自然就是答案;用优先队列来控制过滤深度较大枝条,进行剪枝。

敲了一下,BFS + 优先队列   15ms  一Y,相信假设在ans的存储上优化一下,把较小的ans优先储存,0ms非常轻松

送一特殊数据:

3 3

.a.

x#.

.r.

打印 4

BFS + 优先队列  代码例如以下

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
const int N = 210;
using namespace std; struct node
{
int x, y,ans;
friend bool operator <(const node &a,const node &b)
{
return a.ans>b.ans;
}
};
int n, m;
char ma[N][N];
bool vis[N][N];
int sx, sy;
int mv[4][2] = {{-1,0},{0,1},{0,-1},{1,0}}; void BFS(int sx,int sy)
{
priority_queue<node> q;
node f, t;
f.x = sx, f.y = sy, f.ans = 0;
vis[sx][sy] = true;
q.push(f);
while(!q.empty())
{
t = q.top();
if(ma[t.x][t.y]=='r')
{
cout<<t.ans<<endl;
return ;
}
q.pop();
for(int i=0; i<4; i++)
{
f.x = t.x +mv[i][0];
f.y = t.y +mv[i][1];
if(!vis[f.x][f.y]&&0<=f.x&&f.x<n&&0<=f.y&&f.y<m&&ma[f.x][f.y]!='#')
{
if(ma[f.x][f.y]=='x')
{
f.ans = t.ans+2;
q.push(f);
}
else
{
f.ans = t.ans+1;
q.push(f);
}
vis[f.x][f.y] = true;
}
}
}
cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
bool flag = 0;
for(int i=0; i<n; i++)
{
scanf("%s",ma[i]);
if(flag)
continue;
for(int j=0; j<m; j++)
{
if(ma[i][j]=='a')
{
sx=i;sy=j;flag = true;
break;
} }
}
memset(vis,0,sizeof(vis));
BFS(sx,sy);
}
return 0;
}

双向BFS

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
const int N = 210;
using namespace std;
int mapp[N][N];
int vis[N][N];
struct node{
int x,y;
};
int n,m;
char ma[210][210];
int mv[4][2] = {{1,0},{0,1},{0,-1},{-1,0}};
int dis[N][N];
void BFS(int sx,int sy,int ex,int ey)
{ queue<node>q;
node t,f;
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
f.x = sx; f.y = sy;
t.x = ex; t.y = ey;
vis[sx][sy] = 1;
vis[ex][ey] = 2;
q.push(f);
q.push(t);
while(!q.empty())
{
t = q.front();
q.pop();
for(int i = 0;i<4;i++)
{
f.x = t.x + mv[i][0];
f.y = t.y + mv[i][1];
if(0<=f.x && f.x <n && 0<=f.y && f.y<m)
{
if(ma[f.x][f.y]=='#') continue ;
if(!vis[f.x][f.y]&& ma[f.x][f.y]=='x')
{
dis[f.x][f.y] = dis[t.x][t.y] + 2;
q.push(f);
vis[f.x][f.y] = vis[t.x][t.y];
}
else if(!vis[f.x][f.y]&& ma[f.x][f.y]=='.')
{
dis[f.x][f.y] = dis[t.x][t.y] + 1;
q.push(f);
vis[f.x][f.y] = vis[t.x][t.y];
}
else if(vis[f.x][f.y]!=vis[t.x][t.y])
{
printf("%d\n",dis[f.x][f.y]+dis[t.x][t.y] + 1);
return ;
}
}
}
}
cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
}
int main()
{
int t,sx,sy,ex,ey;
while(~scanf("%d%d",&n,&m))
{
for(int i = 0;i<n;i++)
{
scanf("%s",ma[i]);
for(int j = 0;j<m;j++)
{
if(ma[i][j] == 'a')
{
sx = i; sy = j;
}
else if(ma[i][j]=='r')
{
ex = i; ey = j;
}
}
}
BFS(sx,sy,ex,ey);
}
return 0;
}

渣渣

HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)的更多相关文章

  1. HDU 1242 Rescue(BFS),ZOJ 1649

    题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...

  2. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

  3. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

  4. hdu 1242 Rescue(bfs)

    此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...

  5. HDU 1242 Rescue(BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...

  6. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  7. HDU 1242 Rescue (BFS(广度优先搜索))

    Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  8. hdu 1242 Rescue (BFS)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  9. hdu 1242 Rescue(BFS,优先队列,基础)

    题目 /******************以下思路来自百度菜鸟的程序人生*********************/ bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r ...

随机推荐

  1. centos源码编译安装nginx过程记录

    前言:Centos系统编译安装LNMP环境是每来一台新服务器或换电脑都需要做的事情.这里仅做一个记录.给初学者一个参考! 一.安装前的环境 这里用的是centos 7系统. 我们默认把下载的软件放在 ...

  2. win7如何设置自动关机

    如果想设置Win7按照自己意愿自动关机,而又不希望下载安装第三方软件,则可以通过以下两个方法来简单实现. 工具/原料 Windows7操作系统环境 方法1:利用cmd命令 1 打开cmd窗口. 方法一 ...

  3. python数于字符串

    python数于字符串 1.了解数的类型 2.什么事字符串 3.引号的使用与区别 4.转义符 5.自然字符串 6.字符串的重复 7.子字符串 #了解数的类型 1.python中数的类型主要有五种,分为 ...

  4. nginx--提供一键安装脚本

    nginx特点 基于进程池实现的fastcgi 单一进程即可实现处理上千的连接 易于扩展的插件系统 安装篇 *下载源码 curl -O http://nginx.org/download/nginx- ...

  5. ios 布局 素材 待整理

    https://www.cnblogs.com/fxwl/p/5961372.html div区域 8.盒子模型的相关属性 margin(外边距/边界) border(边框) padding(内边距/ ...

  6. 搭建FileZilla

    FileZilla是C/S架构的,有服务端和客户端 客户端下载地址https://www.filezilla.cn/download/client 安装,一般就下一步下一步了. 服务端下载:https ...

  7. 浅谈FFC

    FFC(Flexible Formatting Context) CSS3引入了一种新的布局模型——flex布局(之前有文章介绍过).flex是flexible box的缩写,一般称之为弹性盒模型.和 ...

  8. 百度地图API获取数据

    目前,大厂的服务范围越来越广,提供的数据信息也是比较全的,在生活服务,办公领域,人工智能等方面都全面覆盖,相对来说,他们的用户基数大,通过用户获取的信息也是巨大的.除了百度提供api,国内提供免费AP ...

  9. NOIP2000方格取数(洛谷,动态规划递推)

    先上题目: P1004 方格取数 下面上ac代码: ///如果先走第一个再走第二个不可控因素太多 #include<bits/stdc++.h> #define ll long long ...

  10. ubuntu环境搭建DNS服务器

    1 安装bind9 apt install bind9 2 修改 named.conf.local,添加要解析的域名及对应的域名配置文件 zone "test.cn"{ type ...