题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2717

题解:一维简单BFS,详细看代码,0ms。

 #include<cstdio>
#include<queue>
using namespace std;
const int maxn=;
bool v[maxn];
int n,k;
struct nd{
int x,step;
};
bool check(int x){
if(x<||x>||v[x])return false;
else return true;
}
int bfs(int s){
for(int i=;i<=;i++)v[i]=false;//初始化标记
queue<nd>Q;
nd S;
S.x=s,S.step=;
v[s]=;
Q.push(S);
while(!Q.empty()){
nd now=Q.front();Q.pop();
if(now.x==k)return now.step;
if(check(now.x+)){
nd tmp;tmp.x=now.x+,tmp.step=now.step+;
v[tmp.x]=;
Q.push(tmp);
}
if(check(now.x-)){
nd tmp;tmp.x=now.x-,tmp.step=now.step+;
v[tmp.x]=;
Q.push(tmp);
}
if(check(now.x*)){
nd tmp;tmp.x=now.x*,tmp.step=now.step+;
v[tmp.x]=;
Q.push(tmp);
}
}
return -;
}
int main(){
while(~scanf("%d%d",&n,&k)){
if(n>=k){printf("%d\n",n-k);continue;}//剪枝
int ans=bfs(n);
printf("%d\n",ans);
}
return ;
}

hdu_2717_Catch That Cow_bfs的更多相关文章

  1. HDU_2717_Catch That Cow

    很短的 BFS 队列 HDU_2717_Catch That Cow #include<iostream> #include<algorithm> #include<cs ...

  2. Catch That Cow_bfs

    Catch That Cow 题目大意:FrammerJohn找奶牛,给出n和k.FJ在n处.每次他可以向左移动一格.向右移动一格或者移动到自己当前格子数乘2的地方.求FJ最少移动多少次.其中,FJ和 ...

随机推荐

  1. CFRound#379(div2)

    题目链接:http://codeforces.com/contest/734 A:SB题. #include<cstdio> #include<cstring> #includ ...

  2. attr 和 prop 区别

    jquery 中 attr 和 prop 都表示 "属性",同样是属性为啥还要弄两个! attr 适用于自定义属性 如 定义一个懒加载用的src 栗子 <img class= ...

  3. bitcode 关于讯飞

    在真机调试的时候一直报 ld: '/Users/Chenglijuan/Documents/语音识别/lib/iflyMSC.framework/iflyMSC(IFlyRecognizerView. ...

  4. Linq to Sql 左连接查询

    var query = from t0 in context.ExpressSendMaster join t1 in context.Supplier on t0.SupplierCode equa ...

  5. LoRaWAN_stack移植笔记(一)--RF硬件相关

    和硬件相关的问题 TCXO 的使用 根据SX1276数据手册, 如果使用TCXO,则需要配置RegTcxo寄存器为0x19,代码如下 ``` c void SX1276SetTcxoConfig(vo ...

  6. hdu1035

    #include<stdio.h>#include<string.h>int step,n,m;int a[1010][1010];char map[11][11];void ...

  7. USACO 3.3 Shopping Offers

    Shopping OffersIOI'95 In a certain shop, each kind of product has an integer price. For example, the ...

  8. PyQt界面编程应用与实践

    一个典型的GUI应用程序可以抽象为:主界面(菜单栏.工具栏.状态栏.内容区域),二级界面(模态.非模态),信息提示(Tooltip),程序图标等组成.本篇根据作者使用PyQt5编写的一个工具,介绍如何 ...

  9. Android如何查看应用签名信息

    转自http://www.trinea.cn/android/android-view-signatures/comment-page-1/ 介绍Android如何查看自己的应用签名及三方APK或系统 ...

  10. 支付宝AR实景红包上线不久即遭破解,官方已提高技术门槛

    临近春节,阿里巴巴和腾讯的红包大战可谓下足功夫,上周支付宝推出了AR实景红包,该玩法基于"LBS+AR+红包"的方式,类似与今年火爆全球的AR手游Pekomon Go ,只不过这次 ...