BZOJ 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛

19 篇文章 0 订阅
2 篇文章 0 订阅

宽搜
不解释


#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define g getchar()
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
inline ll read(){
    ll x=0,f=1;char ch=g;
    for(;ch<'0'||ch>'9';ch=g)if(ch=='-')f=-1;
    for(;ch>='0'&&ch<='9';ch=g)x=x*10+ch-'0';
    return x*f;
}
inline void out(ll x){
    int a[25],wei=0;
    if(x<0)putchar('-'),x=-x;
    for(;x;x/=10)a[++wei]=x%10;
    if(wei==0){puts("0");return;}
    for(int j=wei;j>=1;--j)putchar('0'+a[j]);
    putchar('\n');
}
int n,m,pd[1000000],dis[1000000],dui[1000000],ans[1000000];
int bfs(int n,int m){
    memset(pd,0,sizeof(pd));
    memset(ans,inf,sizeof(ans));
    int tou=1,wei=1;
    dui[1]=n;ans[n]=0;
    for(;tou<=wei;pd[dui[tou++]]=0){
        int u=dui[tou];
        if(u&&ans[u-1]>ans[u]+1){
            ans[u-1]=ans[u]+1;
            if(!pd[u-1]){
                dui[++wei]=u-1;
                pd[u-1]=1;
            }
        }
        if(u<m&&ans[u*2]>ans[u]+1){
            ans[u*2]=ans[u]+1;
            if(!pd[u*2]){
                dui[++wei]=u*2;
                pd[u*2]=1;
            }
        }
        if(u<m*2&&ans[u+1]>ans[u]+1){
            ans[u+1]=ans[u]+1;
            if(!pd[u+1]){
                dui[++wei]=u+1;
                pd[u+1]=1;
            }
        }
    }
}
int main(){
//  freopen("","r",stdin);
//  freopen("","w",stdout);
    n=read();m=read();
    bfs(n,m);
    out(ans[m]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值