2018.03.01
数据结构练习
1.奇怪的电梯
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <stdio.h> 2 #include <math.h> 3 #include <string.h> 4 int n,begin,end; 5 int can=1; 6 int _max=-1; 7 int queue[2001],book[2001]; 8 int boto[2001],step[2001]; 9 int main(){ 10 scanf("%d%d%d",&n,&begin,&end); 11 int i,j; 12 for(i=1;i<=n;i++){ 13 scanf("%d",&boto[i]); 14 book[i]=0; 15 } 16 if(begin==end){ 17 printf("0\n"); 18 return 0; 19 } 20 int head=1,tail=1; 21 queue[head]=begin; 22 book[begin]=1; 23 step[begin]=0; 24 while(can==1){ 25 can=0; 26 int t1,t2; 27 t1=queue[head]+boto[queue[head]]; 28 t2=queue[head]-boto[queue[head]]; 29 if(t1<=n&&book[t1]==0){ 30 step[t1]=step[queue[head]]+1; 31 if(t1==end){ 32 printf("%d\n",step[t1]); 33 return 0; 34 } 35 book[t1]=1; 36 queue[++tail]=t1; 37 can=1; 38 } 39 if(t2>0&&book[t2]==0){ 40 step[t2]=step[queue[head]]+1; 41 if(t2==end){ 42 printf("%d\n",step[t2]); 43 return 0; 44 } 45 book[t2]=1; 46 queue[++tail]=t2; 47 can=1; 48 } 49 if(head<tail)can=1; 50 head++;//出队 51 } 52 printf("%d\n",_max); 53 return 0; 54 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <stdio.h> 2 #include <math.h> 3 #include <string.h> 4 int n,begin,end; 5 int can=1; 6 int _max=-1; 7 int queue[2001],book[2001]; 8 int boto[2001],step[2001]; 9 int main(){ 10 scanf("%d%d%d",&n,&begin,&end); 11 int i,j; 12 for(i=1;i<=n;i++){ 13 scanf("%d",&boto[i]); 14 book[i]=0; 15 } 16 if(begin==end){ 17 printf("0\n"); 18 return 0; 19 } 20 int head=1,tail=1; 21 queue[head]=begin; 22 book[begin]=1; 23 step[begin]=0; 24 while(head<=tail){ 25 int t1,t2; 26 t1=queue[head]+boto[queue[head]]; 27 t2=queue[head]-boto[queue[head]]; 28 if(t1<=n&&book[t1]==0){ 29 step[t1]=step[queue[head]]+1; 30 if(t1==end){ 31 printf("%d\n",step[t1]); 32 return 0; 33 } 34 book[t1]=1; 35 queue[++tail]=t1; 36 } 37 if(t2>0&&book[t2]==0){ 38 step[t2]=step[queue[head]]+1; 39 if(t2==end){ 40 printf("%d\n",step[t2]); 41 return 0; 42 } 43 book[t2]=1; 44 queue[++tail]=t2; 45 } 46 head++;//出队 47 } 48 printf("%d\n",_max); 49 return 0; 50 }