• hdu 1548 A strange lift


    题目连接

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

    A strange lift

    Description

    There is a strange lift.The lift can stop can at every floor as you want, and there is a number $K_i(0 leq K_i leq 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-K_i$ 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 $k_1 = 3, k_2 = 3,k_3 = 1,k_4 = 2, k_5 = 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 leq N,A,B leq 200)$ which describe above,The second line consist $N$ integers $k_1,k_2,....k_n$.
    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 5 5
    3 3 1 2 5
    0

    Sample Output

    3

    简单的bfs。。。

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<vector>
     7 #include<queue>
     8 #include<map>
     9 using std::cin;
    10 using std::cout;
    11 using std::endl;
    12 using std::find;
    13 using std::sort;
    14 using std::map;
    15 using std::pair;
    16 using std::vector;
    17 using std::queue;
    18 #define pb(e) push_back(e)
    19 #define sz(c) (int)(c).size()
    20 #define mp(a, b) make_pair(a, b)
    21 #define all(c) (c).begin(), (c).end()
    22 #define iter(c) decltype((c).begin())
    23 #define cls(arr,val) memset(arr,val,sizeof(arr))
    24 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    25 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
    26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
    27 const int Max_N = 210;
    28 const int dx[] = { -1, 1 };
    29 typedef unsigned long long ull;
    30 bool vis[Max_N];
    31 int N, A, B, arr[Max_N];
    32 struct Node {
    33     int x, s;
    34     Node(int i = 0, int j = 0) :x(i), s(j) {}
    35 };
    36 void bfs() {
    37     cls(vis, 0);
    38     queue<Node> que;
    39     que.push(Node(A, 0));
    40     vis[A] = true;
    41     while (!que.empty()) {
    42         Node tp = que.front(); que.pop();
    43         if (tp.x == B) { printf("%d
    ", tp.s); return; }
    44         rep(i, 2) {
    45             int nx = dx[i] * arr[tp.x] + tp.x;
    46             if (nx < 0 || nx > N || vis[nx]) continue;
    47             que.push(Node(nx, tp.s + 1));
    48             vis[nx] = true;
    49         }
    50     }
    51     puts("-1");
    52 }
    53 int main() {
    54 #ifdef LOCAL
    55     freopen("in.txt", "r", stdin);
    56     freopen("out.txt", "w+", stdout);
    57 #endif
    58     while (~scanf("%d", &N) && N) {
    59         scanf("%d %d", &A, &B);
    60         rep(i, N) scanf("%d", &arr[i + 1]);
    61         bfs();
    62     }
    63     return 0;
    64 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    TreeSet集合的add()方法的源码解析
    Ubuntu下定时任务和自启动任务的部署
    基于Django的独立运行脚本开发
    python做量化交易干货分享
    使用Glide以及OkHttp集成
    Redis实现简单消息队列
    Linux下高并发socket最大连接数
    Ubuntu14.04部署pyspider的过程
    pymongo "ServerSelectionTimeoutError: No servers found yet" 错误的解决
    gitolite服务器部署中的一些坑
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4603120.html
Copyright © 2020-2023  润新知