sdau-2 1013 1014

描述:

诡异电梯

有个电梯,每层有个数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; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值