hdu 1242 Rescue

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242

题意:一个监狱里有路'.',墙'#',守卫'x',要解救的人'a',a的朋友'r',其中'a'仅有一个,其他可有多个。

走一步需要1单位时间,杀守卫需要1单位时间(期间不走动),求能救出'a'的最短时间。

 

思路:深搜。

因为r有多个,所以从a出发找r,找出用时最短的。

vis[i][j]标注到达该点的时间(若该点为x,还包括杀死守卫的时间)。某点可由四个方向到达,所以时间不一定相等,保持最小的那个。

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 using namespace std;
 5 #define N 205
 6 char a[N][N];
 7 int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}}, vis[N][N];
 8 int n, m, ax, ay;
 9 struct Node
10 {
11     int x, y;
12 };
13 queue<Node> q;
14 int main()
15 {
16     while(scanf("%d%d",&n,&m)!=EOF)
17     {
18         while(!q.empty()) q.pop();
19         for(int i=0; i<n; i++)
20             scanf("%s",a[i]);
21         for(int i=0; i<n; i++)
22         {
23             for(int j=0; j<m; j++)
24             {
25                 if(a[i][j]=='a') {  ax = i; ay = j; }
26             }
27         }
28         memset(vis, 0sizeof(vis));
29         int ans = 0//0-没救
30         vis[ax][ay] = 1;
31         Node node, tmp;
32         node.x = ax; node.y = ay;
33         q.push(node);
34         while(!q.empty())
35         {
36             node = q.front(); q.pop();
37             //更新最短时间
38             if(a[node.x][node.y]=='r')
39             {
40                 if(ans==0) ans = vis[node.x][node.y];
41                 else if(ans > vis[node.x][node.y]) ans = vis[node.x][node.y];
42             }
43             for(int i=0; i<4; i++)
44             {
45                 tmp.x = node.x + dir[i][0]; tmp.y = node.y + dir[i][1];
46                 if(tmp.x<0 || tmp.x>=n || tmp.y<0 || tmp.y>=m) continue;
47                 if(a[tmp.x][tmp.y]=='#'continue;
48                 if(vis[tmp.x][tmp.y]==0)
49                 {
50                     vis[tmp.x][tmp.y] = vis[node.x][node.y] + 1;
51                     if(a[tmp.x][tmp.y]=='x') vis[tmp.x][tmp.y]++;
52                 }
53                 else
54                 {
55                     if(a[tmp.x][tmp.y]=='.' && vis[node.x][node.y] + 1 < vis[tmp.x][tmp.y])
56                         vis[tmp.x][tmp.y] = vis[node.x][node.y] + 1;
57                     //找了n久的bug
58                     else if(a[tmp.x][tmp.y]=='r' && vis[node.x][node.y] + 1 < vis[tmp.x][tmp.y])
59                         vis[tmp.x][tmp.y] = vis[node.x][node.y] + 1;
60                     else if(a[tmp.x][tmp.y]=='x' && vis[node.x][node.y] + 2 < vis[tmp.x][tmp.y])
61                         vis[tmp.x][tmp.y] = vis[node.x][node.y] + 2;
62                     else continue;
63                 }
64                 //printf("%d\n",vis[tmp.x][tmp.y]);
65                 q.push(tmp);
66             }
67         }
68         /*
69         for(int i=0; i<n; i++)
70         {
71             for(int j=0; j<m; j++)
72                 printf("%3d ",vis[i][j]);
73             printf("\n");
74         }
75         */
76         if(ans==0) printf("Poor ANGEL has to stay in the prison all his life.\n");
77         else printf("%d\n",ans-1);
78     }
79     return 0;
80 }
View Code

 

posted @ 2013-12-14 11:14  byluoluo  阅读(194)  评论(0)    收藏  举报