HDU 1548 (最基础的BFS了) A strange lift

这是一维的BFS,而且没有什么变形,应该是最基础的BFS了吧

题意:

有这样一个奇葩的电梯,你在第i层的时候你只能选择上或者下Ki层,也就是你只能从第i层到达i+Ki或者i-Ki层。当然电梯最低只能在1层最高只能在n层。

给出起点和终点问最少需要多少次才能到达终点,如果不能到达输出-1

没有什么好解释的了,如此单纯的一道题,只要不是太粗心就能A过去

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6
 7 const int maxn = 200 + 10;
 8 struct Point
 9 {
10     int h;
11     int times;
12 }qu[maxn];
13 int n, from, to, go[maxn], vis[maxn];
14 int head, tail;
15 int dir[2] = {1, -1};
16
17 void BFS(void)
18 {
19     head = 0, tail = 1;
20     qu[0].h = from, qu[0].times = 0;
21     while(head < tail)
22     {
23         if(qu[head].h == to)
24         {
25             printf("%d\n", qu[head].times);
26             return;
27         }
28         for(int i = 0; i < 2; ++i)
29         {
30             int hh = qu[head].h + go[qu[head].h]*dir[i];
31             if(hh>0 && hh<=n && (!vis[hh]))
32             {
33                 vis[hh] = 1;
34                 qu[tail].h = hh;
35                 qu[tail++].times = qu[head].times + 1;
36             }
37         }
38         ++head;
39     }
40     printf("-1\n");
41 }
42
43 int main(void)
44 {
45     #ifdef LOCAL
46         freopen("1548in.txt", "r", stdin);
47     #endif
48
49     while(scanf("%d", &n) == 1 && n)
50     {
51         scanf("%d%d", &from, &to);
52         for(int i = 1; i <= n; ++i)
53             scanf("%d", &go[i]);
54         memset(vis, 0, sizeof(vis));
55         BFS();
56     }
57     return 0;
58 }

代码君

HDU 1548 (最基础的BFS了) A strange lift

时间: 2024-10-24 23:07:43

HDU 1548 (最基础的BFS了) A strange lift的相关文章

HDU 1548 A strange lift(Dijkstra,简单BFS)

题目大意: 电梯有两个选项向上或向下,每层楼有一个参数ki,代表电梯可以再该楼层的基础上向上或向下移动ki层,限制条件是向上不能超过楼层总数n,向下不能少于一.输入总层数n和当前所在层数以及目标层数,然后是n个数分别代表第i层的移动范围.输出最少移动次数,若不可达,输出-1. 解题思路: 1.用Dijkstra算法,首先构建邻接矩阵,注意在构造时,要考虑i-k[i]<1和i+k[i]>n,i代表当前所在层. 1 #include<string.h> 2 #include<st

Hdu 1548 A strange lift(BFS)

Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1548 一道简单的bfs,适合新手.你现在所在的电梯层为一个节点,而你从这个节点可以拜访另外两个节点(电梯向上走为一个节点,电梯向下走有一个节点),而拜访的时候自然也要避免拜访重复,否则会陷入死循环. #include <iostream> #include <queue> using namespace std; const int maxn = 200+10; int N,

HDU 1548 A strange lift(最短路&amp;&amp;bfs)

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26943    Accepted Submission(s): 9699 Problem Description There is a strange lift.The lift can stop can at every floor as you want,

hdu 1240:Asteroids!(三维BFS搜索)

Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3159    Accepted Submission(s): 2106 Problem Description You're in space.You want to get home.There are asteroids.You don't want to hit

HDU 1548 A strange lift【不错的最短路,spfa】

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 21281    Accepted Submission(s): 7786 Problem Description There is a strange lift.The lift can stop can at every floor as you want

HDU 1548 A strange lift 搜索

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11341    Accepted Submission(s): 4289 Problem Description There is a strange lift.The lift can stop can at every floor as you want

HDU 1253 胜利大逃亡(BFS)

#include <iostream> #include <cstdlib> #include <cstdio> #include <queue> #include <cstring> using namespace std; struct node{ int x,y,z,step; }; int ma[51][51][51]; int A,B,C,T; int mv[6][3] = {{1,0,0},{0,1,0},{0,0,1},{-1,0,

hdu 1885 Key Task (三维bfs)

题目 之前比赛的一个题, 当时是崔老师做的,今天我自己做了一下.... 还要注意用bfs的时候  有时候并不是最先到达的就是答案,比如HDU 3442 这道题是要求最小的消耗血量伤害,但是并不是最先到达目标点的路径 就是最小的伤害,因为每一个点的伤害是 不一样的, 这种情况要用优先队列优化, 对伤害优化. 题意:*开始, X出口, b, y, r, g 代表钥匙,分别可以开B, Y, R, G颜色的门, 钥匙可以多次使用.问最短的步骤. 思路:vis[][][]数组开三维,第三维记录状态 是否拿

HDU 1548.A strange lift

A strange lift Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 1548 Description There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N)