题目连接: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. Ubuntu 16.04 搭建Android开发环境

    1.Installing Java sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get inst ...

  2. SpringMVC Memcached 搭建WEB项目缓存框架

    最近做的项目一直在使用memcached作为缓存来缓存各种数据,现在BOSS要在项目上加上缓存.并把任务交给我.便琢磨怎么解决这个问题. 看了很多文章,写的比较详尽靠谱的就是这篇了http://www ...

  3. jquery插件autoComplete自动弹出

    导入 <link rel="stylesheet" href="${ctx }/static/plugins/jQuery-autoComplete-master/ ...

  4. C# CacheHelper

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  5. NLog在MVC中使用

    NLog在MVC中使用 在site根目录新建NLog.config <?xml version="1.0"?> <configuration> <nl ...

  6. web 服务器

    作为一个跨专业转行的我来说,对后台一团浆糊,最近在看php,学的进度比较慢 (1)ApacheApache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上.Apache ...

  7. Android记住密码自动登录的实现

    我采用的是SharedPreferences来存取数据的,所以先简单的介绍一下SharedPreferences SharedPreferences是Android平台上一个轻量级的存储类,主要是保存 ...

  8. vga显示彩条

    vga显示驱动程序可分为扫描行列和行列同步两个部分 //注意:只有在有效区域内给vga赋值才会有颜色变化 assign vga_b = isready ? vga_s[:] :'d0; assign ...

  9. git出错

    查询git的版本是否装对  32 位  64位

  10. Mybatis一对多查询得不到多方结果

    一对多查询:一个年级对应多个学生,现在要查询年级(带学生)信息. 查询结果: [main] INFO com.java1234.service.GradeTest - 查询年级(带学生)[main] ...