zoukankan      html  css  js  c++  java
  • bfs A strange lift

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

    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
     
    题目大意:一共有两个方向的选择,向上or向下,若up则到i+k[i]层的位置,若down,则到i-k[i]的位置,找到最短的从A到B的跳转数
     
    总结:注意搜索后需要标记,最好申明一个标记数组进行标记,其次考虑如何剪枝,注意边界的范围。
     
     1 #include<iostream>
     2 #include<queue>
     3 
     4 using namespace std;
     5 
     6 int k[210],flag[210];
     7 int n,a,b;
     8 
     9 struct node{
    10     int floor;
    11     int step;
    12 };
    13 
    14 int bfs(){
    15     node cur,next;
    16     queue<node> q;
    17     cur.floor = a;
    18     cur.step = 0;
    19     flag[a] = 1;//最开始把这个给忘记了还WA了一次,我傻!
    20     q.push(cur);
    21     while(!q.empty()){
    22         cur = q.front();
    23         q.pop();
    24         if(cur.floor == b){
    25             return cur.step;
    26         }
    27         if(cur.floor+k[cur.floor]<=n && cur.floor+k[cur.floor]>0 && !flag[cur.floor+k[cur.floor]]){
    28             next.floor = cur.floor + k[cur.floor];
    29             flag[cur.floor] = 1;
    30             next.step = cur.step + 1;
    31             q.push(next);
    32         }
    33         if(cur.floor-k[cur.floor]>0 && cur.floor-k[cur.floor]<=n && !flag[cur.floor-k[cur.floor]]){
    34             next.floor = cur.floor - k[cur.floor];
    35             flag[cur.floor] = 1;
    36             next.step = cur.step + 1;
    37             q.push(next);
    38         }
    39     }
    40     return -1;
    41 }
    42 
    43 int main(){
    44     while(cin>>n,n){
    45         cin>>a>>b;
    46         for(int i=1;i<=n;i++){
    47             cin>>k[i];
    48             flag[i]=0;
    49         }
    50         cout<<bfs()<<endl;
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    【Java学习笔记】java.lang包学习
    winform的md5加密
    地铁线路图的设计与实现
    asp.net的jQuery 表格展开伸缩
    关于批量数据更新的问题(C#高性能)
    JQuery用户名无刷新验证
    Linq分页
    用jQuery写的好的动态显示本机时间的代码
    asp.net防止页面刷新弹出“需要重新发送您以前提交的信息”
    aspx中的html代码调用CS文件中的方法
  • 原文地址:https://www.cnblogs.com/pngcui/p/4344080.html
Copyright © 2011-2022 走看看