此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧!

加油!!!优先队列必须要搞定的!

这道题意很简单!自己定义优先级别!

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

===================================================================================

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#include<stdio.h>

#include<string.h>
#include<queue>

using namespace std;

int n,m,dir[4][2]={1,0,0,1,-1,0,0,-1};

char s[1010][1010];

struct node

{


int x,y,step;


friend bool operator<(node a,node b)


{


return a.step>b.step;


}


/*


bool operator<(const node &b) const


{


return step>b.step;


}*/

};

bool judge(int x,int y)

{


if(x>=0&&x<n&&y>=0&&y<m&&s[x][y]!='#')


return true;


return false;

}

int bfs(int x0,int y0)

{


int k,x,y;


priority_queue<node>q;


node cur,next;


cur.x=x0;cur.y=y0;cur.step=0;


q.push(cur);


s[x0][y0]='#';


while(!q.empty())


{


cur=q.top();


q.pop();


for(k=0;k<4;k++)


{


x=next.x=cur.x+dir[k][0];


y=next.y=cur.y+dir[k][1];


next.step=cur.step+1;


if(judge(x,y))


{


if(s[x][y]=='r')


return next.step;


else if(s[x][y]=='.')


q.push(next);


else


{


next.step++;


q.push(next);


}


s[x][y]='#';


}


}


}


return -1;

}

int main()

{


int i,j,x,y,ans;


while(scanf("%d%d",&n,&m)!=EOF)


{


for(i=0;i<n;i++)


scanf("%s",s[i]);


for(i=0;i<n;i++)


{


for(j=0;j<m;j++)


if(s[i][j]=='a')


{


x=i;y=j;


}


if(j<m)


break;


}


ans=bfs(x,y);


if(ans==-1)


printf("Poor ANGEL has to stay in the prison all his life.\n");


else


printf("%d\n",ans);


}


return 0;

}

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242

hdu 1242 Rescue(bfs)的更多相关文章

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

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

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

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

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

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

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

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

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

    题意:X代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙,走过.要花费一秒,走过x要花费2秒,求从起点到终点的最少时间. 析:一看到样例就知道是BFS了吧,很明显是最短路径问题,不过又加了一个条 ...

  6. hdu 1242 Rescue(BFS入门)

    第一次用容器做的BFS题目,题目有个地方比较坑,就是遍历时的方向,比如上下左右能AC,右上左下就WA #include <stdio.h> #include <string.h> ...

  7. HDU 1242 Rescue(优先队列)

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

  8. HDU 1242——Rescue(优先队列)

    题意: 一个天使a被关在迷宫里,她的很多小伙伴r打算去救她.求小伙伴就到她须要的最小时间.在迷宫里有守卫.打败守卫须要一个单位时间.假设碰到守卫必须要杀死他 思路: 天使仅仅有一个,她的小伙伴有非常多 ...

  9. HDU 1242 Rescue (广搜)

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

随机推荐

  1. C#线程(一)

    本篇作为自己简要的笔记和整理,自己英语略渣  就不翻译了 参考http://www.albahari.com/threading 这里有翻译的http://www.cnblogs.com/miniwi ...

  2. 入Lucene的第一个坑

    兴致勃勃的下载了Lucene6的Jar包,打算跑个Demo看下它神奇的魅力,结果一运行就出错了 Exception in thread "main" java.lang.Unsup ...

  3. HDU 2087 剪花布条(模式串在主串中出现的次数主串中子串不可重叠)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 题意:求模式串在主串中出现的次数,与模式串匹配的子串之间不可重叠. 思路:用kmp算法解决,在匹 ...

  4. Creating a Broker (创建代理)

    1,CMD中运行 2,apollo的目录结构. bin  执行相关的脚步. etc  保存实例的配置文件 data  存储消息的文件 log 日志 tmp 临时的文件 3,Broker Configu ...

  5. SQL中 and or优先级问题

    资源来源:http://www.linuxidc.com/Linux/2012-03/56267.htm 刚刚在项目中遇到这样一个问题,SQL语句如下: select * from LOAN_BACK ...

  6. 高效操作DOM

    DOM树与Render树 这个应该都是知道的.就是用户请求HTML下来后,浏览器渲染引擎的基本工作中两个概念. copy一张图,流程大概就是:解析html构建DOM树,渲染树构建,渲染树布局,绘制渲染 ...

  7. HDU 3307 Description has only two Sentences

    数学实在是差到不行了…… #include <cstdio> #include <cstring> #include <algorithm> #include &l ...

  8. HDOJ 1423 Greatest Common Increasing Subsequence(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1423 思路分析:[问题定义]给定两个序列A[0, 1,..., m]和B[0, 1, ..., n], ...

  9. 使用线程 在shell上同步动态显示当前系统时间

    //创建一个用于刷新当前系统时间的线程 new Thread() { public void run() { // 此处为另外一个单独线程,非UI线程 Display dis=shell.getDis ...

  10. servlet response 中文乱码

    先,response返回有两种,一种是字节流outputstream,一种是字符流printwrite. 申明:这里为了方便起见,所有输出都统一用UTF-8编码. 先说字节流,要输出“中国" ...