ZOJ 1649 Rescue BFS (搜索)
2015-07-27 15:03
736 查看
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.)
Input
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.
Output
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."
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
Sample Output
13
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.)
Input
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.
Output
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."
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; int a[4]={0,1,0,-1},b[4]={1,0,-1,0}; struct node { int i; int j; int t; }; int TIME[250][250]; int n,m; char map[250][250]; queue<node>qq; const int maxn=99999999; void BFS() { node temp; while(!qq.empty()){ temp=qq.front(); qq.pop(); for(int i=0;i<4;i++){ int x=temp.i+a[i]; int y=temp.j+b[i]; if(x>=0&&y>=0&&x<n&&y<m&&map[x][y]!='#'){ node tt; tt.i=x; tt.j=y; tt.t=temp.t+1; if(map[x][y]=='x') tt.t++; if(tt.t<TIME[x][y]){ TIME[x][y]=tt.t; qq.push(tt); } } } } } int main() { char ch; int i,j; int di,dj; node start; while(scanf("%d%d",&n,&m)!=EOF){ for(i=0;i<n;i++){ for(j=0;j<m;j++){ TIME[i][j]=maxn; cin>>ch; map[i][j]=ch; if(ch=='r'){ start.i=i; start.j=j; start.t=0; } if(ch=='a'){ di=i; dj=j; } } } qq.push(start); BFS(); if(TIME[di][dj]<maxn) printf("%d\n",TIME[di][dj]); else printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0; }
........
Sample Output
13
相关文章推荐
- epoll 或者 kqueue 的原理是什么?
- IOS:UITextField输入中文时限制长度
- POJ 3368.Frequent values(RMQ和线段树)
- POJ 2442.Sequence
- UITextfield设置Placeholder颜色 控件 内边距、自适应高度
- 自动获取UILabel的宽度高度
- 2.1-特殊权限之guid
- 2.0-特殊权限之suid
- Android开发规范(编码+性能+UI)
- iOS UITableView的使用 (选自oschina)
- The Unique MST----poj1679次小生成树
- poj 1679 The Unique MST
- ${pageContext.request.contextPath}和request.contextPath的区别 分类: jsp el表达式 2013-07-17 15:50 7184人阅读 评论
- [SoapUI] SOAP UI-Groovy Useful Commands
- Xcode6 storyboard new push segue 后的视图控制器没有navigation item bug.
- iOS UIStatusBar字体颜色及隐藏动画
- managedQuery过时的解决办法
- Hibernate 连接数据库,取出query.list()集合中的元素
- [单调队列] hdu 3415 Max Sum of Max-K-sub-sequence
- hdu 5312 Sequence