HDOJ 1242 Rescue 优先队列+bfs
来源:互联网 发布:淘宝学徒招聘 编辑:程序博客网 时间:2024/05/21 10:34
题目大意:
给你一个n*m的二维矩阵,包含墙壁,路,守卫,a,和r(可以不止一个),守卫所在的地方可以通过,但解决他需要花费1单位的时间,从一个点走向旁边的点也需要1单位的时间。求至少有一个r走到a的最短的时间。
题目分析:
因为r不只有一个,所以应该反过来从a搜索到r。由于通过守卫的时间需要两个单位,应用优先队列,让时间少的先行。
源代码:
#include<iostream>#include<queue>using namespace std;char map[201][201];int dir[4][2]={0,1,0,-1,1,0,-1,0};int n,m,ax,ay,flag;struct node{ int x,y,step; friend bool operator < (node a,node b) { return a.step>b.step; }}w,p;int bfs();int main(){ while(cin>>n>>m) { flag=0; getchar(); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>map[i][j]; if(map[i][j]=='a') { ax=i; ay=j; } } getchar(); } int min; min=bfs(); if(flag==1) cout<<min<<endl; else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; } return 0;}int bfs(){ priority_queue<node> q; w.x=ax;w.y=ay;w.step=0; while(!q.empty()) q.pop(); q.push(w); while(!q.empty()) { p=q.top(); q.pop(); if(map[p.x][p.y]=='r') { flag=1; return p.step; } for(int i=0;i<4;i++) { w.x=p.x+dir[i][0]; w.y=p.y+dir[i][1]; w.step=p.step+1; if(w.x>=0&&w.x<n&&w.y>=0&&w.y<m&&map[w.x][w.y]!='#') { if(map[w.x][w.y]=='x') w.step++; if(map[w.x][w.y]!='r') map[w.x][w.y]='#'; q.push(w); } } }}
当前水平评级:5
0 0
- HDOJ 1242 Rescue (BFS+优先队列)
- HDOJ 1242 Rescue 优先队列+bfs
- hdoj 1242 Rescue 【BFS + 优先队列】
- HDOJ 1242 Rescue(bfs+优先队列)
- hdoj 1242 Rescue (bfs+优先队列)
- HDOJ-1242(Rescue)(bfs+优先队列)
- hdoj 1242 Rescue (bfs 优先队列)
- HDOJ 1242 Rescue(BFS + 优先队列)
- HDOJ 1242 Rescue(优先队列+BFS)
- HDOJ 1242 Rescue (BFS + 优先队列)
- HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)
- hdu 1242Rescue(bfs+优先队列)
- hdu 1242 Rescue (优先队列+bfs)
- HDU 1242 Rescue(BFS +优先队列)
- hdu 1242 Rescue(bfs+优先队列)
- 1242Rescue (优先队列BFS)
- HDU 1242 Rescue 优先队列+BFS
- HDU 1242 Rescue(BFS,优先队列)
- 运维笔记-snmp的安装配置
- Eclipse常用快捷方式
- nginx打包rpm
- ansible常用命令
- Android Canvas练习(9)自已绘分割突出效果的饼图(Pie Chart)
- HDOJ 1242 Rescue 优先队列+bfs
- java深入阅读书单
- KVM的HVM虚拟机使用非串口方式建立virsh console 连接
- leo学习系列之四——leo基础用法算子进阶
- 高并发系统设计
- 符合东方国际功夫好看好看个符号的说法是的话
- Drupal 7 模块开发 建立模块帮助信息(hook_help)
- Orchard 基于 ASP.NET MVC 技术的免费开源内容管理系统
- jira tomcat