hdu - 1242 Rescue (优先队列+bfs)

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

感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了。

但是别人说要用优先队列来保证时间最优,我倒是没明白,步数最优跟时间最优不是等价的吗?就算士兵要花费额外时间,可是既然先到了目标点那时间不也一定是最小的?

当然用优先队列+ a去搜索r是最稳妥的。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <iostream>
 5 using namespace std;
 6
 7 struct maze
 8 {
 9     int x,y,t;
10     bool operator < (const maze a) const
11     {
12         return t>a.t;
13     }
14 };
15 int n,m,time;
16 bool flag;
17 char field[210][210];
18 int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
19 void bfs(maze s)
20 {
21     priority_queue<maze>que;
22     que.push(s);
23     while(!que.empty())
24     {
25         maze e=que.top();
26         que.pop();
27         for(int i=0;i<4;i++)
28         {
29             s.x=e.x+dir[i][0];
30             s.y=e.y+dir[i][1];
31             s.t=e.t+1;
32             if(s.x>=0&&s.x<n&&s.y>=0&&s.y<m&&field[s.x][s.y]!=‘#‘)
33             {
34                 if(field[s.x][s.y]==‘r‘)
35                 {
36                     flag=1;time=s.t;return;
37                 }
38                 else if(field[s.x][s.y]==‘x‘) s.t++;
39
40                 //printf("%d %d %d\n",s.x,s.y,s.t);
41                 field[s.x][s.y]=‘#‘;
42                 que.push(s);
43             }
44         }
45     }
46 }
47
48 int main()
49 {
50     //freopen("a.txt","r",stdin);
51     maze s;
52     while(~scanf("%d%d",&n,&m))
53     {
54         getchar();
55         for(int i=0;i<n;i++)
56         {
57             scanf("%s",field[i]);
58             for(int j=0;j<m;j++)
59             {
60                 if(field[i][j]==‘a‘)
61                 {
62                     s.x=i;
63                     s.y=j;
64                     s.t=0;
65                 }
66             }
67         }
68         //printf("%d %d\n",s.x,s.y);
69         flag=0;
70         field[s.x][s.y]=‘#‘;
71         bfs(s);
72         if(flag) printf("%d\n",time);
73         else printf("Poor ANGEL has to stay in the prison all his life.\n");
74     }
75 }
				
时间: 2024-10-11 17:44:46

hdu - 1242 Rescue (优先队列+bfs)的相关文章

HDU 1242 Rescue(优先队列+bfs)

题目地址:HDU 1242 这个题相比于普通的bfs有个特殊的地方,经过士兵时会额外消耗时间,也就是说此时最先搜到的时候不一定是用时最短的了.需要全部搜一遍才可以.这时候优先队列的好处就显现出来了.利用优先队列,可以让队列中的元素按时间排序,让先出来的总是时间短的,这样的话,最先搜到的一定是时间短的,就不用全部搜一遍了.PS:我是为了学优先队列做的这题..不是为了这题而现学的优先队列.. 代码如下: #include <iostream> #include <stdio.h> #i

hdu 1242:Rescue(BFS广搜 + 优先队列)

Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 14   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Angel was caught by the MOLIGPY

HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 开始以为是水题,想敲一下练手的,后来发现并不是一个简单的搜索题,BFS做肯定出事...后来发现题目里面也有坑 题意是从r到a的最短距离,"."相当时间单位1,"x"相当时间单位2,求最短时间 HDU 搜索课件上说,这题和HDU1010相似,刚开始并没有觉得像剪枝,就改用  双向BFS   0ms  一Y,爽! 网上查了一下,神牛们竟然用BFS+

HDU 1242 Rescue营救 BFS算法

题目链接:HDU 1242 Rescue营救 Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16524    Accepted Submission(s): 5997 Problem Description Angel was caught by the MOLIGPY! He was put in prison by

ZOJ 1649 &amp;&amp; HDU 1242 Rescue (BFS + 优先队列)

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

hdu 1242 Rescue (BFS+优先队列)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1242 这道题目我是用BFS+优先队列做的.听说只用bfs会超时. 因为这道题有多个营救者,所以我们从被营救者开始bfs,找到最近的营救者就是最短时间. 先定义一个结构体,存放坐标x和y,还有到达当前点(x,y)消耗的时间. struct node { int x,y; int time; friend bool operator < (const node &a,const node &

HDU 1242 -Rescue (双向BFS)&amp;amp;&amp;amp;( BFS+优先队列)

题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出事...后来发现题目里面也有坑 题意是从r到a的最短距离,"."相当时间单位1,"x"相当时间单位2,求最短时间 HDU 搜索课件上说,这题和HDU1010相似,刚開始并没有认为像剪枝,就改用  双向BFS   0ms  一Y,爽! 网上查了一下,神牛们居然用BFS+优

[ACM] HDU 1242 Rescue (优先队列)

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:

HDOJ/HDU 1242 Rescue(经典BFS深搜)

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