标签:
到目的地的最短时间
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
Sample Output
13
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 using namespace std; 7 8 char map[220][220] ; 9 bool visit[220][220] ; 10 11 int n , m ; 12 int MIN ; 13 14 15 16 void dfs(int x , int y , int sum) 17 { 18 if (x<0 || y<0 || x>= n || y>= m) 19 return ; 20 if (map[x][y] == ‘#‘) 21 return ; 22 if (sum >= MIN) 23 return ; 24 if (visit[x][y] == 1) 25 return ; 26 if (map[x][y] == ‘r‘) 27 { 28 if (sum < MIN) 29 MIN = sum ; 30 return ; 31 } 32 if (map[x][y] == ‘x‘) 33 sum++ ; 34 visit[x][y] = 1 ; 35 dfs(x+1,y,sum+1) ; 36 dfs(x-1,y,sum+1) ; 37 dfs(x,y+1,sum+1) ; 38 dfs(x,y-1,sum+1) ; 39 visit[x][y] = 0 ; 40 } 41 42 int main() 43 { 44 // freopen("in.txt","r",stdin) ; 45 46 47 while (scanf("%d %d" , &n , &m) !=EOF) 48 { 49 if (n == 0 && m == 0) 50 break ; 51 memset(visit ,0 ,sizeof(visit)) ; 52 int i , j ; 53 int bx , by ; 54 int sum = 0 ; 55 56 for (i = 0 ; i < n ; i++) 57 { 58 for (j = 0 ; j < m ; j++) 59 { 60 cin>>map[i][j]; 61 } 62 63 } 64 for (i = 0 ; i < n ; i++) 65 { 66 for (j = 0 ; j < m ; j++) 67 { 68 if (map[i][j] == ‘a‘) 69 { 70 bx = i ; 71 by = j ; 72 break ; 73 } 74 } 75 76 } 77 78 MIN = INT_MAX ; 79 dfs(bx,by,sum) ; 80 if (MIN != INT_MAX) 81 printf("%d\n" , MIN) ; 82 else 83 printf("Poor ANGEL has to stay in the prison all his life.\n") ; 84 85 } 86 87 return 0; 88 }
标签:
原文地址:http://www.cnblogs.com/-Buff-/p/4507237.html