1242 Rescue
题目详情:
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 49369 Accepted Submission(s): 16614
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
题目大意:
解题思路:
所以需要反向遍历。因为我们并不知道朋友的数量如果每个点都要去枚举的话,可能会超时,所以这时候我们就需要用到剪枝操作,我们直接从终点开始遍历,只要遇到朋友的位置就直接返回,这样就会快的很多,这里我们用一个优先队列去操作一番,每次都取出最小的,看看能不能满足要求,一旦满足要求就直接返回。
AC代码:
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int dir_x[4]={0,0,1,-1};
int dir_y[4]={1,-1,0,0};
class location{public:int x;int y;int steps;friend bool operator <(location l_1,location l_2){return l_1.steps>l_2.steps;}
};
bool limit(int x,int y,int n,int m){if(x<=0||y<=0||x>n||y>m) return 0;else return 1;
}
location BFS(vector<vector<char>>& map,int x_start,int y_start,int n,int m){location l_current,l_next;priority_queue<location> q_1;vector<vector<int>> sign(210,vector<int>(210,1));l_current.x=x_start;l_current.y=y_start;l_current.steps=0;q_1.push(l_current);while(!q_1.empty()){l_current=q_1.top();q_1.pop();sign[l_current.x][l_current.y]=0;if(map[l_current.x][l_current.y]=='r'){return l_current;}for(int i=0;i<4;++i){l_next.x=l_current.x+dir_x[i];l_next.y=l_current.y+dir_y[i];if(sign[l_next.x][l_next.y]&&limit(l_next.x,l_next.y,n,m)&&map[l_next.x][l_next.y]!='#'){sign[l_next.x][l_next.y]=0; if(map[l_next.x][l_next.y]=='x'){l_next.steps=l_current.steps+2;}else l_next.steps=l_current.steps+1;q_1.push(l_next);}}}l_current.steps=-1;return l_current;
}
int main(){int n,m;while(cin>>n>>m){vector<vector<char>> map(210,vector<char>(210,0));int x_start,y_start,x_end,y_end;for(int i=1;i<=n;++i){for(int j=1;j<=m;++j){cin>>map[i][j];if(map[i][j]=='a'){x_start=i;y_start=j;}}}location res=BFS(map,x_start,y_start,n,m);if(res.steps==-1) cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;else cout<<res.steps<<endl;}return 0;
}
1242 Rescue相关推荐
- HDU 1242 Rescue BFS+优先队列
题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=1242 #include <stdio.h> #include <stri ...
- 1242 Rescue BFS
#include<iostream> #include<string> #include<string.h> #include<stdio.h> #in ...
- BFS HDOJ 1242 Rescue
题目传送门 题意:从r走到a,遇到x多走一步,问最小走到a的步数 分析:因为r有多个,反过来想从a走到某个r的最小步数,简单的BFS.我对这题有特殊的感情,去年刚来集训队时肉鸽推荐了这题,当时什么都不 ...
- HDU 1242 Rescue
bfs问题. Angel有被关在监狱,她有非常多朋友要去救她. #表示墙,.表示路,x表示警卫,r表示她的朋友. 因为可能有非常多朋友,可是Angel仅仅有一个,所以搜索起点设为Angel.仅仅要找到 ...
- (step4.2.3)hdu 1242(Rescue——BFS)
题目大意:friends用最短的时间去救angel '.'表示通道 '#'表示墙壁 'x'表示guard.走一格要一单位时间,杀死一个guard要一个单位时间. 如果可以救求最短时间,否则按要求输出 ...
- hdu1242 Rescue DFS(路径探索题)
hdu1242 Rescue DFS(路径探索题) 这里我定义的路径探索题指 找某路能够到达目的地,每次走都有方向,由于是探索性的走 之后要后退 那些走过的状态都还原掉 地址:http://acm.h ...
- 杭电OJ分类题目(1)
原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(1) HDU Introduction HDU ...
- 拯救天使 (BFS)
题目: 1242 Rescue 1 //这是一个比较标准的bfs,没有经过任何优化,但是思路比较清晰,容易看懂. 2 #include <iostream> 3 #include < ...
- *【HDU - 1242 】 Rescue (反向dfs,或bfs)
题干: Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N ...
最新文章
- 5、优化MySQL服务器
- richedit line insertion error什么意思_大连 LINE 是怎样的一个公司?
- Android 自己定义View须要重写ondraw()等方法
- 使用Spring Boot和Spring MVC自定义HttpMessageConverters
- 论文浅尝 | Open world Knowledge Graph Completion
- 矩阵的逆、伪逆、左右逆,最小二乘,投影矩阵
- linux4.9下alsa架构,[Alsa]4, wm8524 Kernel音频子系统入口
- 带你认识传统语音识别技术
- 动态更新 HTML 内容 —— AJAX
- idea redis图形化_5.13redis图形化工具---idea中配置redis密码
- 再谈 Formsville
- RGB与YUV格式(四)
- onload同时执行多个事件
- 计算机毕业设计之社区自行车租赁管理系统
- ArcGIS基本使用介绍
- HYSPLIT 教程 有关记录
- HTML制作菜鸟教程首页
- 2022-2028全球与中国三维扫描仪市场现状及未来发展趋势
- Ubuntu 网络限速
- 美国80后恶搞希拉里与名人发短信场景