hdu1548

来源:互联网 发布:编程语言执行效率排行 编辑:程序博客网 时间:2024/05/29 18:57
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 53 3 1 2 50
Sample Output
3
解题思路:
这道题虽然就是一个bfs,开始的时候也是正常写,但是没有做vis标记,当时不觉得需要用vis(但是实际上是需要的,因为如果重复走了一个台阶的话,就不是最优的情况了,虽然能得出答案)交了之后,就mle,于是加了限制条件剪枝,但是没有考虑清楚加错了,最后还是老实加了vis数组,其实就是一个很简单的题目,一直想偏,一直也过不了
代码:
#include<iostream>#include<cstring>#include<queue>#include<cmath>using namespace std;int n,beginn,endd;struct node{    int pos;    int go;    int num;};node a[250];int vis[250];int bfs(node now){    queue<node> q;    q.push(now);    vis[now.pos] = 1;    node help;    node help1;    node help2;    while(!q.empty())    {       // cout<<"here"<<endl;        help = q.front();        q.pop();        if(help.pos==endd)            return help.num;        help1.pos = help.pos+help.go;        if(help1.pos==endd)            return help.num+1;        if(help1.pos>=1&&help1.pos<=n&&!vis[help1.pos])        {            help1.go = a[help1.pos].go;            help1.num = help.num+1;                q.push(help1);                vis[help1.pos] = 1;        }        help2.pos = help.pos-help.go;        if(help2.pos==endd)            return help.num+1;        if(help2.pos>=1&&help2.pos<=n&&!vis[help2.pos])        {           // cout<<"here"<<endl;            help2.go = a[help2.pos].go;            help2.num = help.num+1;                q.push(help2);                vis[help2.pos] = 1;        }    }    return -1;}int main(){    while(cin>>n)    {        memset(vis,0,sizeof(vis));        if(n==0)            break;        cin>>beginn;        cin>>endd;        for(int i = 1;i<=n;i++)        {            cin>>a[i].go;            a[i].pos = i;            a[i].num = 0;        }        node c;        c.go = a[beginn].go;        c.pos = beginn;        c.num = 0;       int re =  bfs(c);       cout<<re<<endl;    }}