zoukankan      html  css  js  c++  java
  • HDU 1548.A strange lift

    A strange lift
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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
     
     
     
     
    BFS题目
    直接套用模板即可
     
    其中有一点是如果"DOWN"到负数楼层,则不下降层数,而非降至1楼;"UP"同理(我觉得没人会读错吧……)
     
     1 /*
     2 By:OhYee
     3 Github:OhYee
     4 Email:oyohyee@oyohyee.com
     5 Blog:http://www.cnblogs.com/ohyee/
     6 
     7 かしこいかわいい?
     8 エリーチカ!
     9 要写出来Хорошо的代码哦~
    10 */
    11 
    12 #include <cstdio>
    13 #include <algorithm>
    14 #include <cstring>
    15 #include <cmath>
    16 #include <string>
    17 #include <iostream>
    18 #include <vector>
    19 #include <list>
    20 #include <queue>
    21 #include <stack>
    22 using namespace std;
    23 
    24 //DEBUG MODE
    25 #define debug 0
    26 
    27 //循环
    28 #define REP(n) for(int o=0;o<n;o++)
    29 
    30 const int maxn = 205;
    31 int k[maxn];
    32 int N;
    33 
    34 int BFS(int s,int v) {
    35     if(s == v)
    36         return 0;
    37 
    38     queue<int> Q;
    39     bool visited[maxn];
    40     memset(visited,false,sizeof(visited));
    41     int dis[maxn];
    42     memset(dis,0,sizeof(dis));
    43 
    44     Q.push(s);
    45     visited[s] = true;
    46     while(!Q.empty()) {
    47         int th = Q.front();
    48         Q.pop();
    49 
    50         //达到终点
    51         if(th == v)
    52             break;
    53 
    54         //拓展节点
    55         int next;
    56         for(int i = -1;i == -1 || i == 1;i += 2) {
    57             next = th + i * k[th];
    58             if(next > N || next <= 0)
    59                 continue;
    60             if(!visited[next]) {
    61                 Q.push(next);
    62                 visited[next] = true;
    63                 dis[next] = dis[th] + 1;
    64             }
    65         }
    66 
    67     }
    68 
    69     if(dis[v])
    70         return dis[v];
    71     else
    72         return -1;
    73 }
    74 
    75 bool Do() {
    76     int s,v;
    77     if(scanf("%d%d%d",&N,&s,&v),N == 0)
    78         return false;
    79     REP(N)
    80         scanf("%d",&k[o + 1]);
    81     printf("%d
    ",BFS(s,v));
    82     return true;
    83 }
    84 
    85 int main() {
    86     while(Do());
    87     return 0;
    88 }
  • 相关阅读:
    75. InputStreamReader和OutputStreamWriter(转换流--字节流转换成字符流)
    74. 编码与解码
    73. PrintStream(打印流)
    72.Properties(配置文件)
    71 Serializable(序列化和反序列化)
    70. SequenceInputStream(文件合并)
    Rabin-Karp指纹字符串查找算法
    优先队列
    版本管理工具svn简介
    php 2038年问题
  • 原文地址:https://www.cnblogs.com/ohyee/p/5389459.html
Copyright © 2011-2022 走看看