HDU 1548 A strange lift 奇怪的电梯(BFS,水)

题意:有一座电梯,其中楼层从1~n,每层都有一个数字k,当处于某一层时,只能往上走k层,或者下走k层。楼主在a层,问是否能到达第b层?

思路:在起点时只能往上走和往下走两个选择,之后的每层都是这样,那么就类似于二叉树。每个节点就是对应的层,因为有可能碰到循环的层,比如1跳到3,3跳回1,这样使得无限循环,所以加个vis数组标记是否遍历过即可。

 1 #include <iostream>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <deque>
 6 using namespace std;
 7 const int N=2008;
 8 int n, a, b;
 9 int q[N];
10 deque<int> que;
11 bool vis[N];
12
13 int cal()
14 {
15     if(a==b)    return 0;
16     que.clear();
17
18
19     int cnt=0;
20     que.push_back(a);
21     while(!que.empty())
22     {
23         int siz=que.size();
24         for(int i=0; i<siz; i++)
25         {
26             int cur=que.front();
27             que.pop_front();
28             if(cur+q[cur]==b||cur-q[cur]==b)
29                 return cnt+1;
30
31             if(cur+q[cur]<=n && !vis[cur+q[cur]] )    //没有遍历过的,小于上限的才考虑
32             {
33                 que.push_back(cur+q[cur]);
34                 vis[cur+q[cur]]=1;
35             }
36             if(cur-q[cur]>0 && !vis[cur-q[cur]] )    //没有遍历过的,大于0的才考虑
37             {
38                 que.push_back(cur-q[cur]);
39                 vis[cur-q[cur]]=1;
40             }
41         }
42         cnt++;
43     }
44     return -1;
45 }
46
47
48 int main()
49 {
50     //freopen("input.txt", "r", stdin);
51
52     while(cin>>n,n)
53     {
54         memset(q,0,sizeof(q));
55         memset(vis,0,sizeof(vis));
56
57         cin>>a>>b;
58         for(int i=1; i<=n; i++)
59             cin>>q[i];
60
61         printf("%d\n",cal());
62     }
63
64     return 0;
65 }

AC代码

时间: 2024-10-09 23:50:01

HDU 1548 A strange lift 奇怪的电梯(BFS,水)的相关文章

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 (dijkstra算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目大意:升降电梯,先给出n层楼,然后给出起始的位置,即使输出从A楼道B楼的最短时间. 注意的几点 (1)每次按一下,只能表示上或者是下,然后根据输入的看是上几层或者是下几层. (2)注意不能到底不存在的楼层. 详见代码. 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 const int IN

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

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 1548 A strange lift Dijkstra+SPFA算法AC

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

hdu 1548 A strange lift (dijkstra)

A strange liftTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 32529    Accepted Submission(s): 11664 Problem DescriptionThere is a strange lift.The lift can stop can at every floor as you want,

HDU 1548 A strange lift——BFS

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <queue> 7 #define sc(x) scanf("%d",&(x)) 8 #define pf(x) printf("%d\n", x) 9 #def