dfs(最佳路径)
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): 43328 Accepted Submission(s): 14783
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
Author
CHEN, Xue
Source
题意:多个r(friends)救a(angle),‘x’代表守卫可以选择干掉他,但需要多花1分钟。
问最块要多久能救a(angle)。
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <algorithm> #include <iostream> #include<cstdio> #include<string> #include<cstring> #include <stdio.h> #include<queue> #include <string.h> #define INF 0x3f3f3f3f using namespace std; char s[309][309]; int ans ; int dir[4][2] = {{0,-1},{0,1},{1,0},{-1,0}}; int n , m ; int vis[309][309]; void dfs(int x , int y , int w) { if(ans <= w) { return ; } if(s[x][y] == 'r') { ans = min(ans , w); return ; } vis[x][y] = 1 ;//一条路径的开始 for(int i = 0 ; i < 4 ; i++) { int xx = x + dir[i][0]; int yy = y + dir[i][1]; if(vis[xx][yy] || s[xx][yy] == '#' || xx < 0 || xx >= n || yy < 0 || yy >= m) { continue ; } //vis[xx][yy] = 1;错误标记 if(s[xx][yy] == 'x') dfs(xx , yy , w+2); else dfs(xx , yy , w+1); //vis[xx][yy] = 0 ;//错误标记 } vis[x][y] = 0 ;//一条路径的结束,准备回溯。 } int main() { while(~scanf("%d%d" , &n , &m)) { int x , y ; for(int i = 0 ; i < n ; i++) { for(int j = 0 ; j < m ; j++) { cin >> s[i][j] ; if(s[i][j] == 'a') { x = i , y = j ; } } } ans = INF ; dfs(x , y , 0); if(ans != INF) { cout << ans << endl ; } else { cout << "Poor ANGEL has to stay in the prison all his life." << endl; } } return 0 ; }