HDU 1242 Rescue
2015-07-14 15:41
465 查看
[align=left]Problem Description[/align]
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
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.)
[align=left]Input[/align]
First line contains two integers stand for N and M. 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.
[align=left]Output[/align]
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
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.)
[align=left]Input[/align]
First line contains two integers stand for N and M. 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.
[align=left]Output[/align]
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
[align=left]Sample Input[/align]
7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
[align=left]Sample Output[/align]
13 用队列做 全是WAWAWA………… 然后百度一下 用的是优先队列 长知识了 队列求出来的只是最优的步数 并不是时间#include <iostream> #include <queue> #include <cstdio> #include <string> #include <cstring> using namespace std; char map[205][205]; int xx[4] = {1,-1,0,0}; int yy[4] = {0,0,1,-1}; bool visit[205][205]; int n,m; struct point { int x; int y; int step; friend bool operator < (point a, point b) { return a.step > b.step; } }a,b; int bfs() { int flag = 0; memset(visit,false,sizeof(visit)); visit[a.x][a.y] = true; priority_queue<point> q; q.push(a); while(!q.empty()) { b = q.top(); q.pop(); if(map[b.x][b.y] == 'r') { flag = b.step; break; } for(int i = 0; i < 4; i++) { a.x = b.x + xx[i]; a.y = b.y + yy[i]; a.step = b.step + 1; if(!visit[a.x][a.y]&&map[a.x][a.y]!='#') { visit[a.x][a.y] = true; if(map[a.x][a.y]=='x') a.step = a.step + 1; q.push(a); } } } return flag; } int main() { while(cin>>n>>m) { for(int i=0;i<=n+1;++i) map[i][0]=map[i][m+1]='#'; for(int i=0;i<=m+1;++i) map[0][i]=map[n+1][i]='#'; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { cin>>map[i][j]; if(map[i][j]=='a') { a.x = i; a.y = j; a.step = 0; } } } int step = bfs(); if(step) { cout<<step<<endl;; } else { cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;; } } return 0; }
相关文章推荐
- flash builder授权问题解决
- LeetCode_96 Unique Binary Search Trees
- OSGI Blueprint(8)
- RapidMiner Studio之GUI多语言支持
- RapidMiner studio之GUI源码分析
- uitableview cell实现性能优化
- [Thu, 9 Jul 2015 ~ Tue, 14 Jul 2015] Deep Learning in arxiv
- Android浏览器(一):Webkit项目与Chromuim项目
- NSMutableURLRequest和NSURLConnection用Post方式上传照片
- uilabel实现自动换行
- POJ 1458 Common Subsequence-dp-(最长公共子序列模型)
- OSGI Blueprint(6)
- StringBuilder与StringBuffer的区别(转)
- ui开头的控件和ns开头的控件有什么区别iOS
- 快速排序 Quick Sort
- UI1_UITableViewHomeWork
- 1007. Maximum Subsequence Sum (25)
- Selenium学习笔记之003:Web UI自动化知识点整理
- [@Controller]3 详解@CookieValue,@PathVariable,@RequestBody,@RequestHeader,@RequestParam
- radio 通过数据库中的value值 自动获取选中状态