hdu 1242 Rescue(BFS,优先队列,基础)
/******************以下思路来自百度菜鸟的程序人生*********************/
bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r’即为所求
需要注意的是这题宽搜时存在障碍物,遇到’x’点是,时间+2,如果用普通的队列就
并不能保证每次出队的是时间最小的元素,所以要用优先队列,第一次用优先队列,还不熟练哇
优先队列(priority_queue)的基本操作:
empty(); 队列为空返回1
pop(); 出队
push(); 入队
top(); 返回队列中优先级最高的元素
size(); 返回队列中元素的个数
/**************************************************************/
修改了学妹的代码,增加了优先队列,成功AC~
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
char map[][];
bool visit[][];
int xx[]={,-,,};
int yy[]={,,,-};
int n,m,x,y;
struct node
{
int x,y;
int time;
friend bool operator < (const node &a,const node &b)
{
return a.time>b.time;
}
};
void bfs(int &min)
{
priority_queue<node>q;
int i;
node step,temp;
step.x=x;
step.y=y;
step.time=;
visit[x][y]=true;
q.push(step);
while(!q.empty())
{
step=q.top();
q.pop();
if(map[step.x][step.y]=='r')
{
min=step.time;
return ;
}
else if(map[step.x][step.y]!='r')
{
for(i=;i<;i++)
{ if(map[step.x+xx[i]][step.y+yy[i]]!='#' && !visit[step.x+xx[i]][step.y+yy[i]])
{
if(map[step.x+xx[i]][step.y+yy[i]]=='x')
{
temp.x=step.x+xx[i];
temp.y=step.y+yy[i]; temp.time=step.time+;
}
else
{
temp.x=step.x+xx[i];
temp.y=step.y+yy[i];
temp.time=step.time+;
}
visit[temp.x][temp.y]=true;
q.push(temp);
}
}
}
}
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
int i,j;
int min=;
memset(visit,false,sizeof(visit));
for(i=;i<=n+;++i)
map[i][]=map[i][m+]='#';
for(i=;i<=m+;++i)
map[][i]=map[n+][i]='#';
for(i=;i<=n;++i)
{
for(j=;j<=m;j++)
{
cin>>map[i][j];
if(map[i][j]=='a')
{
x=i;
y=j;
}
}
}
bfs(min);
if(min<)
printf("%d\n",min);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return ;
}
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(优先队列)
题目来源: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by ...
- 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 (BFS)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 1242 rescue (优先队列模板题)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 1242——Rescue(优先队列)
题意: 一个天使a被关在迷宫里,她的很多小伙伴r打算去救她.求小伙伴就到她须要的最小时间.在迷宫里有守卫.打败守卫须要一个单位时间.假设碰到守卫必须要杀死他 思路: 天使仅仅有一个,她的小伙伴有非常多 ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- 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就能过了 ...
随机推荐
- SpringMvc入门三----控制器
在传统的Spring MVC开发方法中,必须在Bean配置文件中为每个控制器类配置实例和请求映射和让每个控制器类去实现或者扩展特定于框架的接口或者基类,不够灵活. 如果Spring MVC可以自动侦测 ...
- nyoj_t218(Dinner)
描述 Little A is one member of ACM team. He had just won the gold in World Final. To celebrate, he dec ...
- how to debug thread cpu 100%
when we write a program, cpu and memory usages are very important to indicate the stability of the p ...
- Request.IsLocal与Request.Url.IsLoopback的区别
均在服务器上访问时: http://localhost:17810 Request.IsLocal => trueRequest.Url.IsLoopback => true http:/ ...
- winform:无法引用其他类库,dll,using等个人看法【图】
在项目类库中已经引用了相关了类库,生成解决方案也没问题,但是到了后置代码,通过using引用其他类库的时候,再生成解决方案或者生成单个类库,就会报“未能找到类型或命名空间“xxx"(是否缺少 ...
- Sql 执行删除修改之前添加备份
backyw备份滴数据库名称,w20151124sendmaster 表名称 select * into backyw..w20151124sendmaster from Logistics.E ...
- setEllipsize(TruncateAt where)
void android.widget.TextView.setEllipsize(TruncateAt where) public void setEllipsize (TextUtils.Trun ...
- Jquery插件收集
移动端滚动条插件iScroll.js http://www.cnblogs.com/starof/p/5215845.html http://www.codeceo.com/article/35-jq ...
- MySQL 5.7.11 重置root密码
.修改/etc/my.conf,添加参数skip-grant-tables .重启mysql service mysqld stop service mysqld start .用root 直接登录 ...
- 【转】Eazfuscator.NET 3.3中混淆化需要注意的一些问题
对于DLL,Eazfuscator.NET默认不会混淆化任何公共成员,因为类库的公共成员很有可能被外界调用,而对于EXE的程序集,所有类型都可能被混淆化.注意上面这句话有一个“可能”,因为Eazfus ...