码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1242 到目的地的最短时间 dfs

时间:2015-05-16 01:19:48      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

到目的地的最短时间

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 }
View Code

 

hdu 1242 到目的地的最短时间 dfs

标签:

原文地址:http://www.cnblogs.com/-Buff-/p/4507237.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!