hdu 1242(BFS+优先队列)

摘要:
)要进入网格。我们假设上下左右移动需要1个单位的时间,而上下移动也要1个单位时间。而且,我们也需要一个单位时间来完成所有的防护。您必须计算接近Angel的最小高度。输入第一行包含两个整数和NandM。接下来的n行,每行都有M个字符。“.”代表道路,“a”代表天使,和“r”代表每个天使朋友。处理到文件末尾。输出每个测试用例,您的程序应该输出一个整数,代表所需的一半。如果不存在这样的成员,您应该输出包含“PoorANGEL has stay in the life”的数据。示例输入78#。####。#.a#。#..r.#。#x……#。#。#……#……#####。#,2003年10月的描述:一位公主被俘。公主有很多朋友。A代表公主。R代表公主的朋友。#代表墙代表鲁X,守卫需要2秒才能击败守卫。现在让我们问问你的朋友谁能尽快救出公主。因为有不止一个朋友,我们可以通过BFS从公主那里搜索。因为击败后卫需要很长时间,所以我们选择了优先队列。每次我们进入最小的队列时,错误的代码都会通过dfs。它还应该通过1#include<iostream>2#include<csdio>3#include<cmath>4#include<cs ring>5#include<algorithm>6#include<queue>7#include<map>8#include>9#include˂vector>10#include>cstdlib>11#include˂string>12#defineps0.00000000113typedeflonglongll;14种长LL型;15使用namespacestd;16组分N=1000+10;17英寸[N][N];18英寸,m;19charmp[N][N];20int_x[4]={0,1,-1,0};21int_y[4]={1,0,0,-1};22结构节点{23intx,y;24inttime;25friendboolopperb.time;27}28}a,b;29优先级_队列<节点>q;30intjudge{31ifreturn0;32ifretrn0;33ifreturn 0;34return1;35}36intBFS{37a.time=0;38a.x=x;a.y=y;39q.push;40vis[x][y]=1;41while(!
                                         Rescue

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

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
Author
CHEN, Xue
Source
ZOJ Monthly, October 2003
题意:一个公主被抓  公主有很多个朋友  在图上  a代表公主  r 代表公主的朋友 #代表墙  .代表路   x代表守卫
打败守卫花费2秒  其它一秒   现在问你那个朋友能最快解救公主
因为朋友不止一个  我们可以BFS从公主开始搜 由于打败守卫花的时间长  我们选择优先队列  每次去最小 
这题目数据很水  错的代码都能过  dfs应该也能过
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<map>
 8 #include<set>
 9 #include<vector>
10 #include<cstdlib>
11 #include<string>
12 #define eps 0.000000001
13 typedef long long ll;
14 typedef unsigned long long LL;
15 using namespace std;
16 const int N=1000+10;
17 int vis[N][N];
18 int n,m;
19 char mp[N][N];
20 int _x[4]={0,1,-1,0};
21 int _y[4]={1,0,0,-1};
22 struct node{
23     int x,y;
24     int time;
25     friend bool operator < (node a,node b){
26         return a.time>b.time;
27     }
28 }a,b;
29 priority_queue<node>q;
30 int judge(int x,int y){
31     if(x<0||x>=n||y<0||y>=m)return 0;
32     if(vis[x][y]==1)return 0;
33     if(mp[x][y]=='#')return 0;
34     return 1;
35 }
36 int BFS(int x,int y){
37     a.time=0;
38     a.x=x;a.y=y;
39     q.push(a);
40     vis[x][y]=1;
41     while(!q.empty()){
42         b=q.top();
43         q.pop();
44         for(int i=0;i<4;i++){
45             int xx=b.x+_x[i];
46             int yy=b.y+_y[i];
47             if(judge(xx,yy)){
48                 vis[xx][yy]=1;
49                 if(mp[xx][yy]=='x')a.time=b.time+2;
50                 else if(mp[xx][yy]=='r')return (b.time+1);
51                 else
52                     a.time=b.time+1;
53                 a.x=xx;
54                 a.y=yy;
55                 q.push(a);
56             }
57         }
58     }
59     return -1;
60 }
61 int main(){
62     while(scanf("%d%d",&n,&m)!=EOF){
63         while(!q.empty())q.pop();
64         memset(vis,0,sizeof(vis));
65         int x,y;
66         for(int i=0;i<n;i++)
67         for(int j=0;j<m;j++){
68             cin>>mp[i][j];
69             if(mp[i][j]=='a'){
70                 x=i;y=j;
71                 //a.x=x;a.y=y;a.time=0;
72             }
73         }
74         int ans=BFS(x,y);
75         if(ans==-1)
76             printf("Poor ANGEL has to stay in the prison all his life.
" );
77         else
78             printf("%d
",ans);
79     }
80 }

免责声明:文章转载自《hdu 1242(BFS+优先队列)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇OPENQUERY (Transact-SQL),跨数据库操作。linux硬盘的分区、格式化、挂载以及LVM下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

最大堆(优先队列)基本概念,即一个完整建立,插入,删除代码

堆(优先队列)priority queue特殊的队列,取出元素的顺序是依照元素的优先权(关键字)大小,而出元素进入队列的先后顺序操作:查找最大值(最小值),删除(最大值) 数组:链表:有序数组:有序链表: 采用二叉搜索树? NO 采用完全二叉树 YES堆的连个特性结构性:用数组表示的完全二叉树:有序性:任一结点的关键字是其字树所有结点的最大值(或最小值)...

AStar 启发函数设计(老物)

作为我出山的第一篇日志,怎么也得写篇对得起我身份和地位的文章吧? 先容我吐槽一下不小心发的贴图,那个只是我不小心收藏了隔壁兄弟班的课表就别大家这么热情的 BB 我感到很有压力,额,废话不多说,立刻进入正题吧。 简单说一下 AStar (A*)算法,这是一种根据启发函数图遍历算法雏形。 举个栗子,如果你身处迷宫,但你知道出口的方向,那么你应该会尝试往出口方向...

C语言刷 堆(优先队列)

703. 数据流中的第 K 大元素 /* 小根堆 */ typedef struct { int heapCapacity; int heapSize; int *heap; } KthLargest; /* 堆顶下标: 0; parent: (k-1)/2; leftChild: 2*k + 1; rightChild: 2*k...

c++优先队列(priority_queue)用法详解

介绍:   普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除。 在优先队列中,元素被赋予优先级。当访问元素时,具有最高优先级的元素最先删除。优先队列具有最高级先出 (first in, largest out)的行为特征。 首先要包含头文件#include<queue>, 他和queue不同的就在于我们可以自定义其中数据的优...

利用分支限界法求解单源最短路(Dijkstra)问题

分支限界法定义:采用Best fist search算法,并使用剪枝函数的算法称为分支界限法。 分支限界法解释:按Best first的原则,有选择的在其child中进行扩展,从而舍弃不含有最优解的分支,不断重复这一过程,直到找到答案或者判定无解。 分支界限法常常用到优先队列来选择最佳扩展节点,有时也会用到普通队列,以先进先出为原则来进行筛选。 单源最短路...