POJ 3278

嗯……很多博客都做了详细介绍和解释。

http://blog.csdn.net/freezhanacmore/article/details/8168265

等等

#include<map>
#include<set>
#include<list>
#include<stack>
#include<deque>
#include<queue>
#include<vector>
#include<sstream>
#include<iomanip>
#include<iostream>
#include<math.h>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<functional>
#define CPY(A,B)memcpy(A,B,sizeof(A))
#define fin freopen("in.txt","r",stdin)
#define fout freopen("out.txt","w",stdout)
typedef long long LL;
typedef unsigned long long uLL;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
const LL INFF=0x3f3f3f3f3f3f3f3fLL;
const double OO=1e20;
const double EPS=1e-9;
const double PI=acos (-1.0);
int dx[]= {0,1,0,-1};
int dy[]= {1,0,-1,0};
using namespace std;
int gcd (const LL &a,const LL &b) {
    return b==0?a:gcd (b,a%b);
}
inline int dcmp (double a,double b) {
    if (fabs (a-b) <EPS) { return 0; }
    return a<b?-1:1;
}
const int M=200200;
int n,k,ans,vis[M];
void SS () {
    queue<int> Q;
    Q.push (n);
    vis[n]=1;
    while (!Q.empty() ) {
        int now=Q.front();
        Q.pop();
        if (now==k) {
            printf ("%d\n",vis[now]-1);
            return;
        }//分三个方向
        if ( (now*2) <M&&!vis[now*2]) {
            Q.push (now*2);
            vis[now*2]=vis[now]+1;
        }
        if ( (now+1) <M&&!vis[now+1]) {
            Q.push (now+1);
            vis[now+1]=vis[now]+1;
        }
        if ( (now-1) >=0&&!vis[now-1]) {
            Q.push (now-1);
            vis[now-1]=vis[now]+1;
        }
    }
}
int main() {
    while (cin>>n>>k) {
        memset (vis,0,sizeof vis);
        SS ();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值