HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接: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)&&( BFS+优先队列)的更多相关文章
- HDU 1242 Rescue(BFS),ZOJ 1649
题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...
- hdu 1242 Rescue
题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...
- hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...
- hdu 1242 Rescue(bfs)
此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- HDU 1242 Rescue (BFS(广度优先搜索))
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- hdu 1242 Rescue (BFS)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- hdu 1242 Rescue(BFS,优先队列,基础)
题目 /******************以下思路来自百度菜鸟的程序人生*********************/ bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r ...
随机推荐
- 树莓派连接arduino(USB串口通讯)
2018-06-0115:12:19 https://blog.csdn.net/song527730241/article/details/50884890 重要步骤 查看端口:(ttyUSB0或 ...
- NX自动出图
小秀一下战果: 1. 自动添加图框2. 自动添加投影视图3. 自动标注外形尺寸4. 根据工艺要求,自动添加公差5. 自动孔表6. 批量打印 欢迎大家积极吐槽哈 [视频演示] http://v.youk ...
- golang zip 压缩,解压(含目录文件)
每天学习一点go src. 今天学习了zip包的简单使用,实现了含目录的压缩与解压. 写了两个方法,实现了压缩.解压. package ziptest import ( "archive/z ...
- POJ_1018_(dp)
Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28273 Accepted: ...
- MFC获取各类指针句柄
最近有些人在问MFC编程一些要点,有一些句柄的获取.指针的获取是常见的问题,本文将对这些问题做以解释,参考了前人的笔录(见reference),希望能够帮助大家更方便地进行MFC程序开发. 一般我们使 ...
- sort 及lambda表达式定制排序
void CDrawCircle::OnBnClickedBtnSelectinfo() { // TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); BeginEditor ...
- ES6字符串模板
这里做个简单的拓展,之前做vue组件时,经常用到拼接字符串,换行时用到\,既费时又麻烦.这里介绍个ES6字符串模板方法 旧版拼接(各种换行拼接) Vue.component('obj-prop',{ ...
- thinkphp 5.0 “No input file specified”问题
最近在用thinkphp5.0,想要试一下强制路由模式,却发现一直报错, 在网上一通乱找,最后发现是因为不能正确识别path_info造成的, 解决方案就是在 public目录下修改 ".h ...
- /etc/updatedb.conf配置文件
[root@localhost ~]# vi /etc/updatedb.conf PRUNE_BIND_MOUNTS = "yes" PRUNEFS = "9p afs ...
- ORACLE 查看当前用户信息(用户,表视图,索引,表空间,同义词,存储过程,约束条件)
1.用户 查看当前用户的缺省表空间 SQL>select username,default_tablespace from user_users; 查看当前用户的角色 SQL>select ...