poj 3278

来源:互联网 发布:ipad淘宝网不能横屏 编辑:程序博客网 时间:2024/05/17 23:53

poj 3278

Catch That Cow
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 43209 Accepted: 13445

Description

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at 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 of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points - 1 or + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

Input

Line 1: Two space-separated integers: N and K

Output

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

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

Source

USACO 2007 Open Silver

第一篇题解,写一道简单的试试水 -_-||

题意:很简单,就是农夫抓牛,告诉你牛的位置和农夫的位置,然后有两种走法,第一种是从当前位置x走到x-1或者x+1,另外一种是走到2*x位置。

题目很简单,没什么说的,直接上代码~

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<queue>using namespace std;#define maxn 1000500struct point {    int x;//表示坐标    int step;//表示当前的步数};point start;queue<point> Q;bool used[maxn];bool flag;int fj, cow;int mint;void bfs( point s ){    while( !Q.empty() )        Q.pop();//记得清空队列,为此贡献n次wa    int x;    Q.push(s);    used[fj] = 1;    flag = 0;    while( !Q.empty() && !flag )//为了节约时间,达到目标位置就break,退出循环。    {        point hd = Q.front(); //       printf("%d\n", hd.x);        Q.pop();        for( int i=0; i<3; i++ )        {            if( i == 0 )                x = hd.x - 1;            else if( i == 1 )                x = hd.x + 1;            else if( i == 2 )                x = hd.x * 2;//三种走法。            if( x>=0 && x <= 1000000 && !used[x] )            {                used[x] = 1;                point t;                t.x = x;                t.step = hd.step + 1;                Q.push( t );                if( x == cow )                {                    mint = t.step;                    flag = 1;                    break;                }            }        }    }}int main(){    while( scanf("%d%d", &fj, &cow) != EOF )    {        if( fj == cow )        {            printf("0\n");            continue;        }        start.x = fj;        start.step = 0;        memset( used, 0, sizeof(used) );        bfs( start );        printf("%d\n", mint);    }    return 0;}


0 0
原创粉丝点击