• 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;
    }
    
  • 相关阅读:
    poj2728 Desert King
    bzoj4289 Tax
    洛谷P4141消失之物
    Code Forces 698A Vacations
    Code Forces 543A Writing Code
    洛谷P1133 教主的花园
    poj3177 Redundant Paths
    bzoj1151 动物园
    bzoj1503 郁闷的出纳员
    bzoj1208 宠物收养所
  • 原文地址:https://www.cnblogs.com/Titordong/p/9594076.html
Copyright © 2020-2023  润新知