ZOJ1649 Rescue 路径深度图算法
来源:互联网 发布:淘宝机器人客服 编辑:程序博客网 时间:2024/06/06 00:57
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
#include <cstdio> #include <cstring>#define inf 0x3f3f3f3fint n, m, dx, dy;char map[210][210];int time[210][210];inline int min(int x, int y) {return x < y ? x : y;}int main(){ while(~scanf("%d %d", &n, &m)){ for(int i = 0; i < n; i++)scanf("%s", map + i);memset(time, 0x3f, sizeof(time));for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)if(map[i][j] == 'r') time[i][j] = 0; else if(map[i][j] == 'a') {dx = i; dy = j;}bool flag = true; int mint, add;while(flag){ flag = false; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) if(map[i][j] != '#'){ if(map[i][j] == 'x') add = 2; else add = 1; mint = time[i][j]; if(i - 1 >= 0 && map[i-1][j] != '#') mint = min(mint, time[i-1][j] + add); if(i + 1 < n && map[i+1][j] != '#') mint = min(mint, time[i+1][j] + add); if(j - 1 >= 0 && map[i][j-1] != '#') mint = min(mint, time[i][j-1] + add); if(j + 1 < m && map[i][j+1] != '#') mint = min(mint, time[i][j+1] + add); if(mint < time[i][j]){ time[i][j] = mint; flag = true; } } }if(time[dx][dy] != inf) printf("%d\n", time[dx][dy]);else printf("Poor ANGEL has to stay in the prison all his life.\n");}return 0;}
- ZOJ1649 Rescue 路径深度图算法
- zoj1649 Rescue
- zoj1649-Rescue(priority_queue)
- zoj1649/hdu 1242(Rescue )
- Rescue zoj1649 优先队列
- ZOJ1649 营救Rescue (BFS)
- 解题报告:hdu1242 zoj1649 Rescue
- 【ZOJ1649 Rescue】 解题报告+代码+思路
- ZOJ1649
- “Rescue(营救),ZOJ1649”的一种解法和疑惑
- 随机生成图,dijkstra算法求最短路径,深度、广度优先历遍【待更新其他算法】
- poj 1122 FDNY to the Rescue! 最短路径 dijkstra算法
- zoj1649 BFS
- 深度优先搜索迷宫路径算法的实现
- 【算法设计与分析基础】深度优先求最短路径
- 深度优先搜索算法(迷宫最短路径问题)
- 递归算法深入浅出五:深度搜索寻找图最短路径
- 深度优先搜索 Tarjan算法 两条不相交的路径
- C语言头文件的编写
- PID算法初步
- OC基础——点语法、@property、id、构造方法
- 先序+中序和中序+后序建树
- Swift(一)
- ZOJ1649 Rescue 路径深度图算法
- <黑马程序员>代理模式总结-仿制简单spring框架
- 软件版本号规则和命名规则
- 移植OpenWrt到RT5350
- Android PagerSlidingTitleIconTabStrip 可以在title旁边加小图标的PagerSlidingTabStrip
- javascript原生的事件扩展
- Win32串口API
- hdu 3861 The King’s Problem (强连通+最小路径覆盖)
- Java连接数据库主要有两种方式