hdu 1242 Rescue(bfs+优先队列)
2015-10-03 22:44
495 查看
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
bfs+优先队列...
bfs能找到最优解的原因是...时间更短的点一定是先访问到先入队...
但是由于守卫的存在...先访问到的节点如果是守卫的话就不是时间更短的节点...
所以这个时候我们要用优先队列...
第一次写.
然后学习了重载<的写法...写在结构体里面和外面的..
感觉q老师...窝只是请教他这个重载在写结构体里面要怎样...他就把kuangbin的模板发了我一份
学到了不少.
还有就是,因为朋友有多个,而小天使只有一个...
不如让小天使去找朋友...逆向思维...
一遍ac,开心.
View Code
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
bfs+优先队列...
bfs能找到最优解的原因是...时间更短的点一定是先访问到先入队...
但是由于守卫的存在...先访问到的节点如果是守卫的话就不是时间更短的节点...
所以这个时候我们要用优先队列...
第一次写.
然后学习了重载<的写法...写在结构体里面和外面的..
感觉q老师...窝只是请教他这个重载在写结构体里面要怎样...他就把kuangbin的模板发了我一份
学到了不少.
还有就是,因为朋友有多个,而小天使只有一个...
不如让小天使去找朋友...逆向思维...
一遍ac,开心.
/************************************************************************* > File Name: code/hdu/1242.cpp > Author: 111qqz > Email: rkz2013@126.com > Created Time: 2015年10月02日 星期五 14时38分17秒 ************************************************************************/ #include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> #include<cctype> using namespace std; #define yn hez111qqz #define j1 cute111qqz #define ms(a,x) memset(a,x,sizeof(a)) #define lr dying111qqz const int dx4[4]={1,0,0,-1}; const int dy4[4]={0,-1,1,0}; typedef long long LL; typedef double DB; const int inf = 0x3f3f3f3f; const int N=2E2+5; char maze ; int m,n; int ans; bool vis ; struct node { int x,y; int d; bool ok () { if (x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&maze[x][y]!='#') return true; return false; } bool hasguard() { if (maze[x][y]=='x') return true; return false; } bool goal() { if (maze[x][y]=='r') return true; return false; } }s; bool operator<(const node &a,const node &b) //重载优先级的<关系...返回true时优先级更小,在队列的更后面. { return a.d>b.d; } bool bfs() { priority_queue<node>q; q.push(s); while (!q.empty()) { node pre = q.top(); q.pop(); // cout<<pre.x<<" "<<pre.y<<" "<<pre.d<<endl; if (pre.goal()) { ans = pre.d; return true; } for ( int i = 0 ; i < 4 ; i++) { node next; next.x = pre.x + dx4[i]; next.y = pre.y + dy4[i]; next.d = pre.d + 1; if (!next.ok()) continue; vis[next.x][next.y] = true; if (next.hasguard()) next.d++; q.push(next); } } return false; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif while(scanf("%d %d",&n,&m)!=EOF){ ms(vis,false); for ( int i = 0 ; i < n ; i++) scanf("%s",maze[i]); for ( int i = 0 ; i < n ; i++) { for ( int j = 0 ; j < m ; j++) { if (maze[i][j]=='a') { s.x = i ; s.y = j ; s.d = 0 ; vis[i][j] = true; break; } } } if (bfs()) { printf("%d\n",ans); } else { puts("Poor ANGEL has to stay in the prison all his life."); } } #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; }
View Code
相关文章推荐
- SPOJ QTREE - Query on a tree(树链剖分)
- iOS大典之UIScrollView
- [java学习笔记]java语言基础概述之转义字符&break&continue
- android--day02(UI、事件)
- iOS图文攻略之UILabel属性
- UITableViewDelegate方法
- 选择Key-Value Store
- Key-Value存储系统简介
- (一三〇)UITextField的光标操作扩展
- (一三〇)UITextField的光标操作扩展
- Permutation Sequence
- iOS图文攻略之UIView 属性
- Leetcode catalogue
- 关于queue和stack没有clear方法
- sqlite采用UUID主键和int型自增主键性能对比测试
- goquery使用
- UITextField  中文输入法
- Android UI 优化——使用HierarchyViewer工具
- 2015/10/3 iOS 笔记 细节 iOS9中UIAlertController的简单使用 ScrollView NSTimer
- Arduino2-库文件及基本函数