http://acm.hdu.edu.cn/showproblem.php?pid=1242
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10020 Accepted Submission(s): 3668
Problem Description
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.)
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.
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题意: 天使被恶魔抓在监狱中 天使的朋友去救她.表示有路可走#表示墙壁 无法通过x表示 卫兵 想要通过 需要花费一个时间打败他a表示 angle的位置r表示朋友的初始位置直接DFS()的话要超时 看了下大神的剪枝真的很好#include<stdio.h> #define max 32000 int n, m, time, visited[202][202], map[202][202]; int dir[4][2]= {1,0,-1,0,0,1,0,-1}; int ax, ay, ex, ey; void dfs(int x, int y, int t) { int i, ch, xn, yn; if(x == ex && y == ey) {// 如若走到目的地 比较花费的时间 取花费最短的时间 if(t < time) time = t; } else { for(i = 0; i <4; ++i) { xn = x + dir[i][0]; yn = y + dir[i][1]; if((visited[xn][yn] == 1 || visited[xn][yn] == 2) && t + visited[xn][yn] <= time) {//如果有路或者有卫兵才能走 另外 要保证走这个点花费的时间小于之前的最短时间才能进行 if(map[xn][yn] == -1 || t + visited[xn][yn] <= map[xn][yn]) {// map==-1 表示没走过这点 t+v<=map 要保证 再次过这点时的时间必须小于等于之前都过此点时所花费的时间 map[xn][yn] = t + visited[xn][yn];//更新map 使经过此点的时间为最小值 ch = visited[xn][yn];//记录过这点的时间 visited[xn][yn] = -1;//标记走过的点不能再次再走 dfs(xn, yn, t + ch);// 继续搜 visited[xn][yn] = ch;//回溯 } } } } } int main() { int i, j; char str[203]; while(scanf("%d %d\n", &n, &m) != EOF) { time = max; for(i = 0; i <= n + 2; ++i) for(j = 0; j <= m + 2; ++j) { map[i][j] = -1; visited[i][j] = -1; } for(i = 1; i <= n; ++i) { gets(str); for(j = 0; j <= m - 1; ++j) if(str[j] == '.') visited[i][j + 1] = 1; else if(str[j] == 'a') { ax = i; ay = j + 1; } else if(str[j] == 'r') { ex = i; ey = j + 1; visited[i][j + 1] = 1; } else if(str[j] == 'x') visited[i][j + 1] = 2; } dfs(ax, ay, 0); if(time != max) printf("%d\n", time); else printf("Poor ANGEL has to stay in the prison all his life.\n" ); } return 0; }