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

    A strange lift

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 1548
    64-bit integer IO format: %I64d      Java class name: Main
     
     
    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 is on floor A,and you want to go to floor B,how many times at least he havt 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...

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 struct node{
    18     int p,step;
    19     node(int x = 0,int y = 0):p(x),step(y){}
    20 };
    21 int N,A,B,k[210];
    22 bool vis[210];
    23 queue<node>q;
    24 int bfs(){
    25     while(!q.empty()) q.pop();
    26     memset(vis,false,sizeof(vis));
    27     vis[A] = true;
    28     node temp(A,0);
    29     q.push(temp);
    30     int to;
    31     while(!q.empty()){
    32         node now = q.front();
    33         q.pop();
    34         if(now.p == B) return now.step;
    35         to = now.p + k[now.p];
    36         if(to <= N && !vis[to]){
    37             vis[to] = true;
    38             q.push(node(to,now.step+1));
    39         }
    40         to = now.p - k[now.p];
    41         if(to > 0 && !vis[to]){
    42             vis[to] = true;
    43             q.push(node(to,now.step+1));
    44         }
    45     }
    46     return -1;
    47 }
    48 int main() {
    49     while(scanf("%d",&N),N){
    50         scanf("%d %d",&A,&B);
    51         for(int i = 1; i <= N; i++)
    52             scanf("%d",k+i);
    53         printf("%d
    ",bfs());
    54     }
    55     return 0;
    56 }
    View Code
  • 相关阅读:
    跨域请求剖析
    MongoDB 创建索引及其他
    MongoDB的常用操作总结
    Vue学习【第四篇】:Vue 之webpack打包工具的使用
    转载:官方Caffe-windows 配置与示例运行
    转载:基于HALCON的模板匹配方法总结
    机器视觉硬件相关
    opencv画出轮廓外接矩形
    转载:approxPolyDP函数
    转载:return *this和return this
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3945732.html
Copyright © 2011-2022 走看看