zoukankan      html  css  js  c++  java
  • HDOJ1548 A strange lift[BFS()]

    A strange lift

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 6056    Accepted Submission(s): 2238


    Problem 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 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
     
    Recommend
    8600
     
     
     
     
     
     
    code:
     1 #include <iostream>   
     2 #include <iomanip>   
     3 #include <fstream>   
     4 #include <sstream>   
     5 #include <algorithm>   
     6 #include <string>   
     7 #include <set>   
     8 #include <utility>   
     9 #include <queue>   
    10 #include <stack>   
    11 #include <list>   
    12 #include <vector>   
    13 #include <cstdio>   
    14 #include <cstdlib>   
    15 #include <cstring>   
    16 #include <cmath>   
    17 #include <ctime>   
    18 #include <ctype.h> 
    19 using namespace std;
    20 
    21 int n,a,b;
    22 int vst[202];
    23 int table[202][202];
    24 
    25 struct node
    26 {
    27     int floor;
    28     int step;
    29 }Node;
    30 
    31 void bfs()
    32 {
    33     int i;
    34     node pre,last;
    35     pre.floor=a;
    36     pre.step=0;
    37     vst[a]=1;
    38     bool flag=false;
    39     queue<node>Que;
    40     Que.push(pre);
    41     while(!Que.empty())
    42     {
    43         pre=Que.front();
    44         Que.pop();
    45         if(pre.floor==b)
    46         {
    47             flag=true;
    48             break;
    49         }
    50         if(pre.step>=n)
    51             break;
    52         for(i=1;i<=n;i++)
    53         {
    54             last.floor=i;
    55             last.step=pre.step+1;
    56             if(table[pre.floor][last.floor]&&vst[last.floor]==0)
    57             {
    58                 vst[last.floor]=1;
    59                 Que.push(last);
    60             }
    61         }
    62     }
    63     if(!flag)
    64     {
    65         printf("-1\n");
    66         return;
    67     }
    68     printf("%d\n",pre.step);
    69 }
    70 
    71 int main()
    72 {
    73     int i;
    74     int temp;
    75     while(~scanf("%d",&n),n)
    76     {
    77         scanf("%d%d",&a,&b);
    78         memset(table,0,sizeof(table));
    79         memset(vst,0,sizeof(vst));
    80         for(i=1;i<=n;i++)
    81         {
    82             scanf("%d",&temp);
    83             if((i-temp)>0)
    84                 table[i][i-temp]=1;
    85             if((i+temp)<=n)
    86                 table[i][i+temp]=1;
    87         }
    88         if(a==b)
    89         {
    90             printf("0\n");
    91             continue;
    92         }
    93         bfs();
    94     }
    95     return 0;
    96 }
  • 相关阅读:
    sql server 镜像操作
    微信测试公众号的创见以及菜单创建
    linux安装redis步骤
    Mysql 查询表字段数量
    linux 链接mysql并覆盖数据
    linux (centos)增删改查用户命令
    CentOS修改用户密码方法
    https原理及其中所包含的对称加密、非对称加密、数字证书、数字签名
    com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. 问题解决方法
    设计模式(三):模板方法模式
  • 原文地址:https://www.cnblogs.com/XBWer/p/2615909.html
Copyright © 2011-2022 走看看