HDU 1548 A strange lift

题目链接:HDU 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) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as you know ,the -2 th floor isn't exist.
Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at least he has to press the button "UP" or "DOWN"?

Input

The input consists of several test cases.,Each test case contains two lines.
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.
A single 0 indicate the end of the input.

Output

For each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can't reach floor B,printf "-1".

Sample Input

5 1 5
3 3 1 2 5
0

Sample Output

3

题意

有这样的一栋楼,楼里每层的电梯都有一个数x,表示只能坐电梯到达当前楼+x层或者当前楼-x层,当然不能超过楼层总数也不能小于0,问,给定起点终点,最快需要坐几次这样的“神奇电梯”可以到达终点,不能的话输出-1。

题解:

这道题的解法也有很多,本来应该是一道最短路的题(雾),但是当时看到就直接想到了DFS的做法,然后就直接上手做了。
从起点开始DFS,两个选择,上或者下x层,做一个标记表示哪层来过了(来过了肯定就不再走一次啦~),然后每次更新到达终点的最小步数,输出。

代码

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;

typedef long long ll;

const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int N = 2e3 + 5;

int A, B;
int n;
int num;
int flag = false;
bool vis[N];
int K[N];
void dfs(int a, int step) {
    if (step > num)
        return;
    if (a == B) {
        if (step < num) {
            flag = true;
            num = step;
        }
    }
    if (a + K[a] <= n && a + K[a] > 0 && !vis[a + K[a]]) {
        vis[a + K[a]] = true;
        dfs(a + K[a], step + 1);
        vis[a + K[a]] = false;
    }
    if (a - K[a] <= n && a - K[a] > 0 && !vis[a - K[a]]) {
        vis[a - K[a]] = true;
        dfs(a - K[a], step + 1);
        vis[a - K[a]] = false;
    }
}

int main() {

    while (cin >> n, n) {
        cin >> A >> B;
        num = INF;
        memset(vis, false, sizeof vis);
        memset(K, 0, sizeof K);
        flag = false;
        for (int i(0); i < n; i++)
            cin >> K[i + 1];
        vis[A] = true;
        dfs(A, 0);
        if (flag)
            cout << num << endl;
        else cout << -1 << endl;
    }
    return 0;
}
全部评论

相关推荐

想踩缝纫机的小师弟练...:不理解你们这些人,要放记录就把对方公司名字放出来啊。不然怎么网暴他们
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 一张图晒出你司的标语 #
4341次浏览 75人参与
# AI面会问哪些问题? #
28026次浏览 559人参与
# 米连集团26产品管培生项目 #
13373次浏览 285人参与
# 你的实习产出是真实的还是包装的? #
20248次浏览 342人参与
# 找AI工作可以去哪些公司? #
9210次浏览 239人参与
# 春招至今,你的战绩如何? #
65577次浏览 583人参与
# 开放七大实习专项,百度暑期实习值得冲吗 #
15274次浏览 221人参与
# 从事AI岗需要掌握哪些技术栈? #
9043次浏览 311人参与
# 中国电信笔试 #
32021次浏览 292人参与
# 你做过最难的笔试是哪家公司 #
33775次浏览 238人参与
# 投递几十家公司,到现在0offer,大家都一样吗 #
340878次浏览 2175人参与
# 哪些公司真双非友好? #
69630次浏览 289人参与
# 阿里笔试 #
178680次浏览 1317人参与
# 机械人避雷的岗位/公司 #
62704次浏览 393人参与
# 小马智行求职进展汇总 #
25133次浏览 80人参与
# 第一份工作一定要去大厂吗 #
14734次浏览 122人参与
# 金三银四,你的春招进行到哪个阶段了? #
22098次浏览 280人参与
# 为了减少AI幻觉,你注入过哪些设定? #
26263次浏览 310人参与
# 应届生第一份工资要多少合适 #
20691次浏览 86人参与
# 沪漂/北漂你觉得哪个更苦? #
9939次浏览 193人参与
# 聊聊你的职场新体验 #
336515次浏览 1895人参与
# HR最不可信的一句话是__ #
6306次浏览 114人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务