码迷,mamicode.com
首页 > 其他好文 > 详细

2018.03.01(数据结构练习)

时间:2018-03-01 11:50:06      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:none   while   技术   scan   图片   queue   ++   gpo   eve   

 

 

技术分享图片
 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 }
View Code
技术分享图片
 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 }
View Code

 

2018.03.01(数据结构练习)

标签:none   while   技术   scan   图片   queue   ++   gpo   eve   

原文地址:https://www.cnblogs.com/yzyl-Leo-wey/p/8487193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!