博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2018.03.01(数据结构练习)
阅读量:4570 次
发布时间:2019-06-08

本文共 2622 字,大约阅读时间需要 8 分钟。

 2018.03.01

数据结构练习

1.奇怪的电梯

1 #include 
2 #include
3 #include
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
View Code
1 #include 
2 #include
3 #include
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

 

转载于:https://www.cnblogs.com/yzyl-Leo-wey/p/8487193.html

你可能感兴趣的文章
Canvas基本绘画学习
查看>>
要习惯用vector代替数组
查看>>
Django ORM 最后操作
查看>>
HDU 1050(贪心)
查看>>
java设计模式之代理模式
查看>>
spring心得2--bean的生命周期@Spring监听器的作用@Spring初始化容器案例分析@web项目使用...
查看>>
顺序栈
查看>>
Glut,程序的基本架构
查看>>
WebStorm使用快速入门
查看>>
Rsync详解
查看>>
【每日一读】Java编程中“为了性能”尽量要做到的一些地方
查看>>
什么是内网、什么是公网、什么是NAT
查看>>
【堆/排序】堆排序的两种建堆方法
查看>>
类的内置方法
查看>>
项目中使用的第三方开源库
查看>>
[idasheng.cn]准备工作
查看>>
Chrome浏览器之 webdriver(Chrome version must be >= 63.0.3239.0)
查看>>
NOIP2009 潜伏者
查看>>
本地预览的vue项目,在githubpage静态展示
查看>>
SC命令---安装、开启、配置、关闭 cmd命令行和bat批处理操作windows服务
查看>>