因为要求的是最少的时间,很明显的是一个利用优先队列的bfs的题目,题目很一般。

#include"iostream"
#include"algorithm"
#include"stdio.h"
#include"string.h"
#include"cmath"
#include"queue"
#define mx 205
using namespace std;
int n,m,sx,sy,ex,ey;
int dir[][]={{,},{,-},{-,},{,}};
char map1[mx][mx];
struct node
{
int x,y,steps;
friend bool operator<(node a,node b)
{
return b.steps<a.steps;
}
};
bool judge(int x,int y)
{
if(x>=&&x<n&&y>=&&y<m&&map1[x][y]!='#') return true;
return false;
}
void bfs()
{
node cur,next;
cur.x=sx;cur.y=sy;cur.steps=;
int i;
priority_queue<node>q;
q.push(cur);
while(!q.empty())
{
cur=q.top();
q.pop();
if(cur.x==ex&&cur.y==ey){cout<<cur.steps<<endl;return;}
for(i=;i<;i++)
{
next.x=cur.x+dir[i][];
next.y=cur.y+dir[i][];
if(judge(next.x,next.y))
{
if(map1[next.x][next.y]=='x')
{
next.steps=cur.steps+;
}
else next.steps=cur.steps+;
map1[next.x][next.y]='#';
q.push(next);
}
}
}
cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
int i,j;
for(i=;i<n;i++)
for(j=;j<m;j++)
{
cin>>map1[i][j];
if(map1[i][j]=='r') {sx=i;sy=j;map1[i][j]='#';}
else if(map1[i][j]=='a') {ex=i;ey=j;map1[i][j]='.';}
}
bfs();
}
return ;
}

hdu Rescue的更多相关文章

  1. hdu Rescue (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242 简单优先队列搜索,自己好久不敲,,,,,手残啊,,,,orz 代码: #include < ...

  2. hdu Rescue 1242

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

  3. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

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

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

  5. hdu 1242 Rescue

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

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

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

  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)

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

  9. 杭电 HDU 1242 Rescue

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 问题:牢房里有墙(#),警卫(x)和道路( . ),天使被关在牢房里位置为a,你的位置在r处,杀死一个警卫 ...

随机推荐

  1. Java for LeetCode 070 Climbing Stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  2. 1.前端笔记之html

    title: 1.前端笔记之HTML date: 2016-04-04 23:21:52 tags: Python categories: Python --- 作者:刘耀 **出处:http://w ...

  3. Android涉及到的设计模式

    转载地址:http://blog.csdn.net/dengshengjin2234/article/details/8502097 1.适配器模式:ListView或GridView的Adapter ...

  4. 在Spring3中,配置DataSource的方法有6种。

    第一种:beans.xml Xml代码 收藏代码<bean id="dataSource" class="org.apache.commons.dbcp.Basic ...

  5. google登录不了解决喽

    大家好,google 每到这个时候就登录不聊了.... 解法: 修改host 文件 下载地址点我

  6. Linux 中文乱码问题

    弄了好久还是乱码 最终方法:进入 vim /etc/vimrc中 原先只有一个 set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936改成: let ...

  7. SQL with as

    姓名 课程 分数 张三 语文 张三 数学 张三 物理 李四 语文 李四 数学 李四 物理 先看下面一个嵌套的查询语句 ) 上面的查询语句使用了一个子查询.虽然这条SQL语句并不复杂,但如果嵌套的层次过 ...

  8. DJANGO的API跨域实现

    Ajax跨域请求报错:XMLHttpRequest cannot load ''. No 'Access-Control-Allow-Origin' header is present on the ...

  9. 烟大 Contest1025 - 《挑战编程》第二章:数据结构 Problem A: Jolly Jumpers(水题)

    Problem A: Jolly Jumpers Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 10  Solved: 4[Submit][Status] ...

  10. .NET 下各种Resource的读取方式

    1) Embedded Resource (Build Action 设置为 Embedded Resource) 在运行时使用GetManifestResourceStream读取 Image.Fr ...