题目连接: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. break位于内循环。作用于外循环

    outer:for(int i = 0; i < 4; ++i)  {       inner:for(int j = 0; j < 4; ++j)       {           S ...

  2. Mysql 常用命令和注意事项

    1. 连接mysql数据库 如果配置了环境变量可以直接运行,如果没有配置环境变量需要在安装目录...\bin下运行, cmd -> mysql -u root -p,然后输入密码: 或者可以运行 ...

  3. NGINX----源码阅读---config配置脚本

    config文件为nginx的配置入口文件. 1. #!/bin/sh # Copyright (C) Igor Sysoev # Copyright (C) Nginx, Inc. LC_ALL=C ...

  4. CSU 1806 Toll

    最短路,自适应$Simpson$积分. 看了别人的题解才知道有个东西叫自适应$Simpson$积分. 有这样一个积分公式:$\int_a^b {f(x)dx}  \approx \frac{{b - ...

  5. Importing Product Images on Magento

    Multiple product images of each type can be imported into Magento, and associated with a specific pr ...

  6. 转:【iOS开发每日小笔记(十一)】iOS8更新留下的“坑” NSAttributedString设置下划线 NSUnderlineStyleAttributeName 属性必须为NSNumber

    http://www.bubuko.com/infodetail-382485.html 标签:des   class   style   代码   html   使用   问题   文件   数据 ...

  7. OSI参考模型初识

    纪念我曾今热爱的数通(^o^). 1.osi参考模型 2.数据的封装和解封装 3.主机和主机间通信

  8. 安卓用canvas画曲线图

    1.新建一个常变量类Constant.java package com.rain.db; import android.graphics.Point; public class Constant { ...

  9. F9 开发之左树右表中的左树

    1 首先在前端应用树树控件 <div class="fui-left"> <div role="head" title="地区选择& ...

  10. 初级AD域渗透系列

      net group /domain 获得所有域用户组列表 net group “domain admins” /domain 获得域管理员列表 net group “enterprise admi ...