- 总时间限制:
- 2000ms 内存限制:
- 65536kB
- 描述
-
农夫知道一头牛的位置,想要抓住它。农夫和牛都位于数轴上,农夫起始位于点N(0<=N<=100000),牛位于点K(0<=K<=100000)。农夫有两种移动方式:
1、从X移动到X-1或X+1,每次移动花费一分钟2、从X移动到2*X,每次移动花费一分钟假设牛没有意识到农夫的行动,站在原地不动。农夫最少要花多少时间才能抓住牛? 输入 - 两个整数,N和K 输出
- 一个整数,农夫抓到牛所要花费的最小分钟数 样例输入
-
5 17
样例输出 -
4 BFS
1 #include
2 3 #define maxn 100000 4 5 using namespace std; 6 7 int s,t,head,tail; 8 9 bool vis[maxn+5];10 11 struct node12 {13 int now,val;14 }que[maxn+5];15 16 int main()17 {18 cin>>s>>t;19 que[tail].now=s;20 que[tail++].val=0;21 vis[s]=1;22 while(head 0)44 {45 que[tail].now=now-1;46 que[tail++].val=val+1;47 vis[now-1]=1;48 }49 head++;50 }51 return 0;52 } 1 #include
2 #include 3 #include 4 5 using namespace std; 6 7 int n,k; 8 int b[100005]; 9 10 struct sop11 {12 int sum;13 int step; 14 }queue[100005];15 16 void bfs(int n,int k)17 {18 int head=0;19 int tail=1;20 queue[tail].sum=n;21 queue[tail].step=0;22 b[n]=1;23 while(head =0))46 {47 tail++;48 queue[tail].sum=queue[head].sum-1;49 b[queue[head].sum-1]=1;50 queue[tail].step=queue[head].step+1;51 }52 } 53 return ; 54 }55 56 int main()57 {58 cin>>n>>k;59 60 bfs(n,k);61 return 0; 62 }