Rescue--hdu1242

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21431    Accepted Submission(s): 7641

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

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.

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

这个题是用广搜写的!但是这个题是有点特殊的,因为遇到X的时候时间是要再加一的!所以可以用优先队列来解决,但是还是可以用普通队列来解决的,方法就是遇到X先加一,然后标记走过,再后来再遇到X直接加一,不再往四周搜索!等于走了两步!详情如下

普通队列版:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 #define ma 210
 6 using namespace std;
 7 char map[ma][ma];
 8 int v[ma][ma],m,n;
 9 int mov[4][2]={1,0,-1,0,0,1,0,-1};
10 struct node
11 {
12     int x,y,step,flag;
13 };
14 bool can(node x)
15 {
16     if(x.x>=0&&x.x<m&&x.y>=0&&x.y<n&&(map[x.x][x.y]!=‘#‘)&&!v[x.x][x.y])
17     return true ;
18     return false;
19 }
20 int bfs(int x,int y)
21 {
22     int i;
23     memset(v,0,sizeof(v));
24     queue<node>q;
25     node ta,tb;
26     ta.x=x;
27     ta.y=y;
28     ta.step=0;
29     ta.flag=0;
30     q.push(ta);
31     while(!q.empty())
32     {
33         ta=q.front();
34         q.pop();
35         if(ta.flag==1)
36         {
37             ta.step++;
38             ta.flag=0;
39             q.push(ta);
40             continue;
41         }//第二次遇到时,flag已经等于1,这时候不能走了,步数要加一,而且flag要重新标记为0
42         if(map[ta.x][ta.y]==‘a‘)
43             return ta.step;
44         for(i=0;i<4;i++)
45         {
46             tb.x=ta.x+mov[i][0];
47             tb.y=ta.y+mov[i][1];
48             tb.step=ta.step+1;
49             if(can(tb))
50             {
51
52                 if(map[tb.x][tb.y]==‘x‘)
53                 tb.flag=1;
54                 else
55                 tb.flag=0;//第一次遇到X就标记为flag=1,否则为0!
56                 v[tb.x][tb.y]=1;
57                 q.push(tb);
58             }
59         }
60     }
61     return -1;
62 }
63 int main()
64 {
65     int i,j,a,b;
66     while(scanf("%d%d",&m,&n)!=EOF)
67     {
68         getchar();
69         for(i=0;i<m;i++)
70         {
71             for(j=0;j<n;j++)
72             {
73                 scanf("%c",&map[i][j]);
74                 if(map[i][j]==‘r‘)
75                 {
76                     a=i;
77                     b=j;
78                 }
79             }
80             getchar();
81         }
82         v[a][b]=1;
83         int rr=bfs(a,b);
84         if(rr==-1)
85         printf("Poor ANGEL has to stay in the prison all his life.\n");
86         else
87         printf("%d\n",rr);
88     }
89     return 0;
90  } 

优先队列版:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 #define maxn 210
 6 using namespace std;
 7 int m,n,v[maxn][maxn],mov[4][2]={1,0,-1,0,0,1,0,-1};
 8 char map[maxn][maxn];
 9
10 struct node
11 {
12     int x,y,step;
13     friend bool operator <(node x,node y)
14     {
15         return x.step>y.step;
16     }
17 };
18 priority_queue<node>q;
19 bool can(node x)
20 {
21     if(!v[x.x][x.y]&&x.x>=0&&x.x<m&&x.y>=0&&x.y<n&&map[x.x][x.y]!=‘#‘)//(map[x.x][x.y]==‘.‘||map[x.x][x.y]==‘x‘))
22     return  true;
23     return false;
24  }
25 int bfs(int x,int y)
26 {
27     int i;
28     node ta,tb;
29     ta.x=x;
30     ta.y=y;
31     ta.step=0;
32     q.push(ta);
33     while(!q.empty())
34     {
35         ta=q.top();
36         q.pop();
37         if(map[ta.x][ta.y]==‘a‘)
38             return ta.step;//到终点就返回队首,用的优先队列所以队首的步数最少!
39         for(i=0;i<4;i++)
40         {
41             tb.x=ta.x+mov[i][0];
42             tb.y=ta.y+mov[i][1];
43             if(can(tb))
44             {
45                 if(map[tb.x][tb.y]==‘x‘)
46                 tb.step=ta.step+2;//遇到X一定要加2
47                 else
48                 tb.step=ta.step+1;//其他加1
49                 v[tb.x][tb.y]=1;
50                 q.push(tb);
51             }
52
53         }
54     }
55     return -1;//找不到就返回-1
56 }
57 int main()
58 {
59     int i,j,a,b;
60     while(scanf("%d%d",&m,&n)!=EOF)
61     {
62         getchar();
63         memset(v,0,sizeof(v));
64         for(i=0;i<m;i++)
65         {
66             for(j=0;j<n;j++)
67             {
68                 scanf("%c",&map[i][j]);
69                 if(map[i][j]==‘r‘)
70                 a=i,b=j;
71             }
72             getchar();
73         }
74         v[a][b]=1;
75         int rr=bfs(a,b);
76         if(rr==-1)
77         printf("Poor ANGEL has to stay in the prison all his life.\n");
78         else
79         printf("%d\n",rr);
80     }
81     return 0;
82 } 
时间: 2024-11-07 05:21:33

Rescue--hdu1242的相关文章

HDU1242 Rescue 【BFS】

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16314    Accepted Submission(s): 5926 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is

hdu1242 Rescue(BFS)

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 21701    Accepted Submission(s): 7745 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is

HDU1242 Rescue

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242 参考博文1:http://blog.csdn.net/iaccepted/article/details/23101875 参考博文2:http://blog.csdn.net/libin56842/article/details/9039351 该题类似于走迷宫,但不完全是,因为在走的过程中会遇到警察,可以选择杀死警察,但要付出相应的代价,时间加1.用走迷宫的算法只是能找到angle到队友最

hdu1242(Rescue)

点击打开链接 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:

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue 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:

poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)

题目链接:poj1122 FDNY to the Rescue! 题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给出火警位置所在的交叉路口 和 一个或多个消防站所处的交叉路口位置.输出要求按消防站到火警位置所需时间从小到大排列,输出信息包括消防站位置(初始位置),火警位置(目标位置),所需时间,最短路径上每个交叉路口. 题解:反向建图,从火警位置求一次最短路,求最短路时记录路径,按时间从小到大输出. 1 #in

hdu1242 优先队列+bfs

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22034    Accepted Submission(s): 7843 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is d

HDU 4057 Rescue the Rabbit (AC自动机+DP)

http://acm.hdu.edu.cn/showproblem.php?pid=4057 Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1482    Accepted Submission(s): 430 Problem Description Dr. X is a biologist,

HDUJ 1242 Rescue 搜索

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15582    Accepted Submission(s): 5656 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is

杭电 1242 Rescue(广搜)

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): 15597    Accepted Submission(s): 5663 Problem Description Angel was caught by the MOLIGPY!