ZOJ 1649 Rescue
来源:互联网 发布:c2c电商市场份额 淘宝 编辑:程序博客网 时间:2024/05/16 00:49
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
第一次见不用标记的BFS,以前还是眼界太窄啊。。。。。。。
#include <stdio.h>#include <string.h>#include <queue>using namespace std;char mp[210][210];int dis[210][210];int bhx[] = {0,0,1,-1};int bhy[] = {1,-1,0,0};struct node{ int x; int y; int step; int tim;};queue <node> q;const int inf = 1 << 28;void bfs(int sx,int sy,int ex,int ey,int n,int m){ while(!q.empty()) q.pop(); node t,f; t.x = sx; t.y = sy; t.step = 0; t.tim = 0; q.push(t); while(!q.empty()) { t = q.front(); q.pop(); for(int i = 0; i < 4; i++) { f.x = t.x + bhx[i]; f.y = t.y + bhy[i]; if(f.x >= 0 && f.x < n && f.y >= 0 && f.y < m && mp[f.x][f.y] != '#') { f.step = t.step + 1; f.tim = t.tim + 1; if(mp[f.x][f.y] == 'x') f.tim++; if(dis[f.x][f.y] > f.tim) { // printf("dis[%d][%d] = %d\n",f.x,f.y,f.tim); dis[f.x][f.y] = f.tim; q.push(f); } } } } if(dis[ex][ey] < inf) printf("%d\n",dis[ex][ey]); else printf("Poor ANGEL has to stay in the prison all his life.\n"); }int main(){ int n,m,sx,sy,ex,ey; while(~scanf("%d%d",&n,&m)) { for(int i = 0; i < n; i++) scanf("%s",mp[i]); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { dis[i][j] = inf; if(mp[i][j] == 'r') { sx = i; sy = j; } else if(mp[i][j] == 'a') { ex = i; ey = j; } } } dis[sx][sy] = 0; bfs(sx,sy,ex,ey,n,m); } return 0;}
- zoj 1649 Rescue
- ZOJ 1649 Rescue
- zoj 1649 Rescue
- ZOJ 1649 Rescue (BFS)
- zoj 1649 rescue (bfs)
- ZOJ-1649-Rescue
- zoj 1649 Rescue
- ZOJ:1649 Rescue
- ZOJ 1649 Rescue
- ACM ZOJ 1649 (Rescue)
- ZOJ(1649)Rescue
- ZOJ 1649 Rescue
- zoj 1649 Rescue
- zoj 1649 Rescue
- ZOJ 1649 : Rescue - BFS
- ZOJ 1649 Rescue
- zoj 1649 Rescue
- zoj 1649 Rescue bfs
- 《C++编程》第二章第6题
- ITK+VTK+QT 读取DICOM序列图像遇到的问题(1)
- 1-3图示RFID系统组成及其功能
- 着色问题(回溯法)
- Linux网络应用编程之交换机概述
- ZOJ 1649 Rescue
- 关于Activity调用finish却不立即执行onDestory的困惑
- C++学习:模板类学习
- UITableView的一些属性
- 编译常见错误解决
- 在window下搭建GNUstep 环境
- java中枚举类型的简单使用
- stl map高效遍历删除的方法
- tableview 设置group之间的距离