1209:Catch That Cow(bfs)

题意:

从一个坐标到另一个坐标的移动方式有三种,即:st-1,st+1,2*st。每移动一步时间是一秒。

给出两个坐标,求得从第一坐标到第二座标的最短时间。

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=100005;
int step[maxn];
int st,ed;
void bfs(){
    int key,t;
    queue<int>que;
    que.push(st);
    while(!que.empty()){
        key=que.front();
        que.pop();
        if(key==ed)
            break;
        t=key+1;
        if(t<maxn&&step[t]==0){
            step[t]=step[key]+1;
            que.push(t);
        }
        t=key-1;
        if(0<=t&&t<maxn&&step[t]==0){
            step[t]=step[key]+1;
            que.push(t);
        }
        t=key*2;
        if(t<maxn&&step[t]==0){
            step[t]=step[key]+1;
            que.push(t);
        }
    }
}
int main ()
{
    scanf("%d%d",&st,&ed);
    memset(step,0,sizeof(step));
    bfs();
    printf("%d
",step[ed]);
    return 0;
}


想的太多,做的太少。
原文地址:https://www.cnblogs.com/pealicx/p/6115667.html