码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 搜索练习 A strange lift

时间:2016-04-17 20:31:45      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

A strange lift

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 122 Accepted Submission(s) : 22
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 &lt;= Ki &lt;= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button &quot;UP&quot; , 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 &quot;DOWN&quot; , 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 &quot;UP&quot;, and you‘ll go up to the 4 th floor,and if you press the button &quot;DOWN&quot;, 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.<br>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 &quot;UP&quot; or &quot;DOWN&quot;?<br>
 

 

Input
The input consists of several test cases.,Each test case contains two lines.<br>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.<br>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
 
简单题意:
  给出N个楼梯up or down的参数, 和所在的楼层,和目标楼层,比如 在1楼按up那么会上升1楼的参数层,求到达目的楼层的最小次数
 
思路分析:
  有目标层数,明显BFS。
 
# include <iostream>
# include <fstream>
# include <cstring>
# include <queue>
using namespace std;
int n, a, b;
int floor[1000];
int is[1000];
struct Info
{
    int k;
    int jishu;
};

int bfs();
int main()
{
    //fstream cin("aaa.txt");
    while(cin >> n)
    {
        if(n == 0)
        break;
        cin >> a >> b;
        memset(is, 0, sizeof(is));
        for(int i = 1; i <= n; i++)
            cin >> floor[i];
        int jishu = bfs();
        cout << jishu << endl;
    }

    return 0;
}
int bfs()
{
    is[a] = 1;
    queue <Info> Q;
    Info info;

    info.k = a;
    info.jishu = 0;

    Q.push(info);
    Info t, tep;
    while(!Q.empty())
    {
        t = Q.front();
        Q.pop();
        if(t.k == b)
        {
            return t.jishu;
        }
        for(int i = -1; i <= 1; i++)
        {
            tep.k = t.k + i * floor[t.k];
            if(tep.k >= 1 && tep.k <= n && !is[tep.k])
            {
                is[tep.k] = 1;
                tep.jishu = t.jishu + 1;
                Q.push(tep);
            }
        }

    }
    return -1;
}

 

HDU 搜索练习 A strange lift

标签:

原文地址:http://www.cnblogs.com/lyf-acm/p/5401898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!