hdu 1242 Rescue(BFS,优先队列,基础)
2014-01-16 19:18
681 查看
题目
/******************以下思路来自百度菜鸟的程序人生*********************/
bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r’即为所求
需要注意的是这题宽搜时存在障碍物,遇到’x’点是,时间+2,如果用普通的队列就
并不能保证每次出队的是时间最小的元素,所以要用优先队列,第一次用优先队列,还不熟练哇
优先队列(priority_queue)的基本操作:
empty(); 队列为空返回1
pop(); 出队
push(); 入队
top(); 返回队列中优先级最高的元素
size(); 返回队列中元素的个数
/**************************************************************/
修改了学妹的代码,增加了优先队列,成功AC~
View Code
/******************以下思路来自百度菜鸟的程序人生*********************/
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[205][205]; bool visit[205][205]; int xx[5]={1,-1,0,0}; int yy[5]={0,0,1,-1}; 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=0; 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=0;i<4;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+2; } else { temp.x=step.x+xx[i]; temp.y=step.y+yy[i]; temp.time=step.time+1; } visit[temp.x][temp.y]=true; q.push(temp); } } } } } int main() { while(~scanf("%d %d",&n,&m)) { int i,j; int min=50000; memset(visit,false,sizeof(visit)); for(i=0;i<=n+1;++i) map[i][0]=map[i][m+1]='#'; for(i=0;i<=m+1;++i) map[0][i]=map[n+1][i]='#'; for(i=1;i<=n;++i) { for(j=1;j<=m;j++) { cin>>map[i][j]; if(map[i][j]=='a') { x=i; y=j; } } } bfs(min); if(min<50000) printf("%d\n",min); else printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0; }
View Code
相关文章推荐
- easyUI 中datatGrid操作
- iOS nsdateformatter AM/PM issue
- PreparedStatementCallback; SQL [insert into person values(?,?,?)]; ORA-01722: 无效数字
- stringstream clear()的疑问 - yuanshuilee的日志 - 网易博客
- UpdatePanel 报错 Microsoft JScript 运行时错误: Sys.WebForms.PageRequestManagerParserErrorException:解决办法
- 开机出现unkown filesystem grub rescue问题解决
- UIImage转化UIColor时造成的内存使用增加的原因
- 关于通过easyui对mysql的模糊查询以及按时间段查询的个人见解
- UICircularSlider for ios
- The selected game is missing one or more required ROM or CHD images 解决方法
- HDU - 1159 - Common Subsequence
- Android4.1中的UI平滑技术
- osg内嵌qt,直接使用q creater可视化生成的Ui文件
- CRM js生成32位的Guid
- CPUID读取有关Cache的信息
- hdu 2062 Subset sequence
- Win7下安装Flash Builder 4.0在进度到达1%便不在动回退到0%
- iOS UITableView 实现上拉及下拉刷新
- UUID在Java中的两种产生方式
- Prerequisites?