题目链接: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. Mysql慢SQL与索引案例

    写在最前 关于慢sql的开启与配置查看之前我整理的文章: http://www.cnblogs.com/hanxiaobei/p/5515624.html 前提准备: tomcat7.x mysql- ...

  2. oracle dos命令

    1.无账户密码登录数据库:sqlplus/nolog 后面不能加分号,否则不能识别 2.登录数据库:sqlplus 3.在sql下测试连接性:conn oracle_name/oracle_passw ...

  3. IT项目为什么失败 --美国IT项目管理硕士笔记(一)

    IT项目为什么失败 什么是项目   项目可以被看作任何一系列的活动和任务.这些活动和任务有一个特定目标需要在特定要求下完成,并有一个明确的开始结束日期和资金限制(如果有).项目需要消耗人力或非人力资源 ...

  4. CAD使用GetAllAppName读所有名称(网页版)

    主要用到函数说明: MxDrawEntity::GetAllAppName 得到所有扩展数据名称,详细说明如下: 参数 说明 [out, retval] IMxDrawResbuf** ppRet 返 ...

  5. 梦想CAD控件系统变量说明

    这里介绍一些常用系统变量有String.double.long.McGePoint3d等类型,其中有部分系统变量是随图纸保存,再次打开时就会读取图纸中的系统变量,有些系统变量不随图纸保存,其作用来控制 ...

  6. JavaScipt30(第八个案例)(主要知识点:canvas)

    承接上文,这是第8个案例,要实现的效果是按住鼠标不放,进行拖动时可以在画布上画出不同粗细不同颜色的曲线. 附上项目链接: https://github.com/wesbos/JavaScript30 ...

  7. 01Struts 2

    Struts 2 Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交 ...

  8. 行内块+calc+margin 三列布局

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. Go:值类型、引用类型

    值类型,变量存的就是值本身: in系列t.float系列.bool.string.数组和struct 引用类型,变量存的是一个地址,这是地址存的才是值本身: 指针.slice.map.chan.int ...

  10. linux ltrace-跟踪进程调用库函数的情况

    当前位置:硬件 | 监测 | 内核 | Shell / 性能监测与优化 /ltrace ltrace命令是用来跟踪进程调用库函数的情况. 语法 ltrace [option ...] [command ...