1209:Catch That Cow(bfs)

来源:互联网 发布:windows rmdir命令 编辑:程序博客网 时间:2024/06/03 22:55

题意:

从一个坐标到另一个坐标的移动方式有三种,即: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\n",step[ed]);    return 0;}


0 0
原创粉丝点击