我是靠谱客的博主 羞涩楼房,最近开发中收集的这篇文章主要介绍HDU1242 Rescue(BFS+优先队列)Rescue,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Rescue

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


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
题意:问最小花时间从起点到终点,其中要到守卫的地方要多花1个时间。
思路:由于其中有若干步要花费2个时间,可以用优先队列存储,走花费时间少的。用DFS枚举的会超时。
收获:优先队列定义结构体优先级。
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 #define maxn 200+10
 8 int n,m,flag;
 9 struct Node
10 {
11     int x,y,cost;
12     bool operator < (const Node &a)const//定义时间花费少的优先级高。
13     {
14         return cost>a.cost;
15     }
16 };
17 Node st,et,f,k;
18 int dx[]={0,0,-1,1};
19 int dy[]={1,-1,0,0};
20 int vis[maxn][maxn];
21 char map[maxn][maxn];
22 void bfs()
23 {
24     priority_queue<Node>q;
25     q.push(st);
26     while(!q.empty())
27     {
28         f=q.top();
29         for(int i=0;i<4;i++)
30         {
31             k.x=f.x+dx[i];
32             k.y=f.y+dy[i];
33             if(!vis[k.x][k.y]&&k.x>=0&&k.x<n&&k.y>=0&&k.y<m)
34             {
35                 vis[k.x][k.y]=1;
36                 k.cost=f.cost+1;
37                 if(map[k.x][k.y]=='x')
38                     k.cost++;
39                 if(k.x==et.x&&k.y==et.y)
40                     {
41                     printf("%dn",k.cost);
42                     return;
43                     }
44                 q.push(k);
45             }
46             }
47             q.pop();
48     }
49     printf("Poor ANGEL has to stay in the prison all his life.n");
50 }
51 
52 int main()
53 {
54     while(~scanf("%d%d",&n,&m))
55     {
56         if(m==0)
57             break;
58         memset(vis,0,sizeof(vis));
59         getchar();
60         for(int i=0;i<n;i++)
61         {
62             for(int j=0;j<m;j++)
63             {
64                 scanf("%c",&map[i][j]);
65                 if(map[i][j]=='#')
66                 {
67                     vis[i][j]=1;
68                 }
69                 if(map[i][j]=='r')
70                 {
71                      st.x=i;
72                      st.y=j;
73                      st.cost=0;
74                 }
75                 if(map[i][j]=='a')
76                 {
77                     et.x=i;
78                     et.y=j;
79                 }
80 
81             }
82             getchar();
83         }
84         bfs();
85     }
86 
87     return 0;
88 }

 

转载于:https://www.cnblogs.com/ZP-Better/p/4698762.html

最后

以上就是羞涩楼房为你收集整理的HDU1242 Rescue(BFS+优先队列)Rescue的全部内容,希望文章能够帮你解决HDU1242 Rescue(BFS+优先队列)Rescue所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(35)

评论列表共有 0 条评论

立即
投稿
返回
顶部