1360:奇怪的电梯(lift)
作者:互联网
注意起点等于终点的情况!
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 using namespace std; 5 const int N=205; 6 int n,a[N],b[N]; 7 queue<int> q; 8 void cnt(){ 9 while(!q.empty()){ 10 int p=q.front(); 11 q.pop(); 12 if(p+a[p]<=n&&(!b[p+a[p]]||b[p+a[p]]>b[p]+1)){ 13 b[p+a[p]]=b[p]+1; 14 q.push(p+a[p]); 15 } 16 if(p-a[p]>0&&(!b[p-a[p]]||b[p-a[p]]>b[p]+1)){ 17 b[p-a[p]]=b[p]+1; 18 q.push(p-a[p]); 19 } 20 } 21 } 22 int main(){ 23 int s,e; 24 cin>>n>>s>>e; 25 for(int i=1;i<=n;i++) 26 scanf("%d",&a[i]); 27 if(s!=e){ 28 q.push(s); 29 cnt(); 30 cout<<(b[e]?b[e]:-1); 31 }else cout<<0; 32 return 0; 33 }
标签:int,pop,电梯,lift,1360,push,include,奇怪 来源: https://www.cnblogs.com/sxrekord/p/strange_elevator.html