sdau-2 1013 1014

来源:互联网 发布:图片视频制作软件 编辑:程序博客网 时间:2024/05/16 11:58

描述:

诡异电梯

有个电梯,每层有个数n,如果是上到那一层,就再上n层,同理,下到那一层,就再下降n层。输入起始,重点,问要最少按几次电梯运行键

输入

5 1 5
3 3 1 2 5
0
输出
3
思路:
基础广搜,分上下两种状态,然后向队列中push就行了。
1013 1014重复
代码:
#include<iostream>#include<cstring>#include<queue>using namespace std;struct lift{    int x;    int t;};int main()           {    //freopen("r.txt","r",stdin);    int ww[2000],c[2000];    lift n1,n2,m;    queue<lift> Q;    int n,a,b,i,e,z;    while(cin>>n&&n!=0)    {        memset(c,0,sizeof(c));        cin>>a>>b;        for(i=1;i<=n;i++)        {            cin>>ww[i];c[i]=0;        }        e=0;        n1.x=a;        n1.t=0;        Q.push(n1);        c[n1.x]=1;        while(!Q.empty())        {            m=Q.front();            Q.pop();            if(m.x==b)            {                e=1;break;            }            n1.x=m.x-ww[m.x];            n2.x=m.x+ww[m.x];            if(n1.x>0&&n1.x<=b&&!c[n1.x])            {                n1.t=m.t+1;                c[n1.x]=1;                Q.push(n1);            }            if(n2.x>0&&n2.x<=b&&!c[n2.x])            {                n2.t=m.t+1;                c[n2.t]=1;                Q.push(n2);            }        }        while(!Q.empty())        {            Q.pop();        }        if(e) cout<<m.t<<endl;        else cout<<"-1"<<endl;    }    return 0;}

0 0
原创粉丝点击