sdutacm-Catch That Cow

来源:互联网 发布:hotel california知乎 编辑:程序博客网 时间:2024/06/08 18:35

Catch That Cow

Time Limit: 2000MSMemory Limit: 65536KB

SubmitStatistic

ProblemDescription

Farmer John has been informedof the location of a fugitive cow and wants to catch her immediately. He startsat a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0≤ K ≤ 100,000) on the same number line. Farmer John has two modes oftransportation: walking and teleporting. * Walking: FJ can move from any pointX to the points X - 1 or X + 1 in a single minute * Teleporting: FJ can movefrom any point X to the point 2 × X in a single minute. If the cow, unaware ofits pursuit, does not move at all, how long does it take for Farmer John toretrieve it?

Input

Line 1: Two space-separatedintegers: N and K

Output

Line 1: The least amount oftime, in minutes, it takes for Farmer John to catch the fugitive cow.

ExampleInput

5 17

ExampleOutput

4

Hint

poj3278有链接提示的题目请先去链接处提交程序,AC后提交到SDUTOJ中,以便查询存档。
The fastest way for Farmer John to reach the fugitive cow is to move along thefollowing path: 5-10-9-18-17, which takes 4 minutes.

Author

#include <iostream>#include<bits/stdc++.h>using namespace std;struct node{int x,y,step;}st;int n,m,flag,book[200000];int bfs(){    queue<node>g;    memset(book,0,sizeof(book));    book[st.x] = 1;    g.push(st);    while(!g.empty())    {        node now = g.front();        g.pop();        if(now.x==m)        return now.step;        for(int j=0;j<3;j++)        {            node next;            if(j==0)            {            next.x = now.x+1;            }            else if(j==1)            {            next.x = now.x-1;            }            else if(j==2)            {            next.x = now.x*2;            }            next.step = now.step+1;            if(next.x==m)            {                return next.step;            }            if(next.x>=0&&next.x<=200000&&!book[next.x])            {                book[next.x] = 1;                g.push(next);            }        }    }    return 0 ;}int main(){    while(~scanf("%d%d",&n,&m))    {        st.x = n;        st.step = 0;        printf("%d\n",bfs());    }    return 0;}/***************************************************User name: jk160505徐红博Result: AcceptedTake time: 4msTake Memory: 944KBSubmit time: 2017-02-15 21:00:28****************************************************/

0 0