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

HDU - 1242 Rescue

时间:2017-07-31 20:50:59      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:case   des   mat   sum   scribe   was   getch   end   namespace   

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.)
InputFirst 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.
OutputFor 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

r要走的a在的地方(反之也可以),#不能走,.路程加1,x路程加2,问最短路径有多长,用BFS。
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<queue>
 4 #include<string.h>
 5 
 6 using namespace std;
 7 
 8 char c[205][205];
 9 int dx[4]={-1,0,0,1};
10 int dy[4]={0,-1,1,0};
11 
12 int x,y,x1,y1,x2,y2;
13 
14 struct node
15 {
16     int x,y,step;
17     friend bool operator < (node a,node b)
18     {
19         return a.step>b.step;
20     }
21 };
22 
23 int BFS()
24 {
25     priority_queue<node> p;
26     node p1,p2;
27     p1.x=x2;p1.y=y2;
28     p1.step=0;
29     p.push(p1);
30     while(!p.empty())
31     {
32         p2=p.top();
33         p.pop();
34         x=p2.x;
35         y=p2.y;
36         if(x==x1&&y==y1)
37             return p2.step;
38         c[p2.x][p2.y]=#;
39         for(int i=0;i<4;i++)
40         {
41             x=p2.x+dx[i];
42             y=p2.y+dy[i];
43             if(c[x][y]!=#)
44             {
45                 p1.x=x;
46                 p1.y=y;
47                 p1.step=p2.step;
48                 p1.step++;
49                 if(c[x][y]==x)
50                     p1.step++;
51                 p.push(p1);
52             }
53         }
54     }
55     return 0;
56 }
57 
58 int main()
59 {
60     while(~scanf("%d%d",&x,&y))
61     {
62     
63     memset(c,#,sizeof(c));
64     for(int i=1;i<=x;i++)
65     {
66         getchar();
67         for(int j=1;j<=y;j++)
68         {
69             scanf("%c",&c[i][j]);
70             if(c[i][j]==a)
71             {
72                 x1=i;
73                 y1=j;
74             }
75             if(c[i][j]==r)
76             {
77                 x2=i;
78                 y2=j;
79             }
80         }
81     }
82     int s=BFS();
83     if(s)
84         cout<<s<<endl;
85         else
86             cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
87     }
88     
89     return 0;
90 }

 

HDU - 1242 Rescue

标签:case   des   mat   sum   scribe   was   getch   end   namespace   

原文地址:http://www.cnblogs.com/xibeiw/p/7265370.html

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