1253:抓住那头牛

http://ybt.ssoier.cn:8088/problem_show.php?pid=1253

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n, k;
 4 struct node{                                //结构体表示数轴位置和所用时间 
 5     int x, t;
 6 };
 7 node que[100010];                            //结构体数组表示队列 
 8 int f, r;                                    //定义队首和队尾 
 9 bool vis[100010];                            //数轴位置是否被访问过 
10 void move(int nx, int ft){                    //判读是否符合入队要求,并入队 
11     if(nx>=0 && nx<=100000 && vis[nx]==0){
12         vis[nx]=1;                            //更改访问状态 
13         r++;                                //队尾后移准备入队 
14         que[r].x=nx;                        //移动位置加入队列 
15         que[r].t=ft+1;                        //移动时间增加 
16     }
17 }
18 int main()
19 {
20     cin>>n>>k;
21     f=r=1;                                //队列初始化 
22     que[r].x=n; que[r].t=0;                //农夫初始位置入队 
23     while(f<=r){
24         int fx=que[f].x, ft=que[f].t;    //获取队首信息 
25         if(fx==k){                        //判读是否到达牛队位置 
26             cout<<ft;
27             break;
28         }
29         move(fx+1, ft);                    //三种移动方式 :方式1 
30         move(fx-1, ft);                    //三种移动方式 :方式2
31         move(fx*2, ft);                    //三种移动方式 :方式3
32         f++;                            //队首后移出队列 
33     }
34     return 0;
35 }
原文地址:https://www.cnblogs.com/tflsnoi/p/13752415.html