题目连接: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. http缓存与cdn相关技术

    阅读目录 一 http缓存 二.Http缓存概念解析 三.cdn相关技术 摘要:最近要做这个主题的组内分享,所以准备了一个星期,查了比较多的资料.准备的过程虽然很烦很耗时间,不过因为需要查很多的资料, ...

  2. DrawingCombiner——CAD图纸批量合并软件

    DrawingCombiner是一款CAD图纸批量合并软件,可以批量合并多个dwg或dxf文件为单个dwg文件,并可以设置合并后的排列方式. 此程序附属MagicTable(可到依云官网下载:http ...

  3. C#函数以及应用

  4. 【IE6的疯狂之八】链接伪类(:hover)CSS背景图片有闪动BUG

    IE6下链接伪类(:hover)CSS背景图片有闪动BUG,主要原因ie会再一次请求这张图片,或者说图片没被缓存. 例如: CSS代码 a:hover{background:url(imagepath ...

  5. 编写CodeMirror Modes详解

    在写新的模式(mode)之前,还是先来概括一下CodeMirror的设计思路.CodeMirror分为三部分:核心,模式,扩展.核心库对外开放了很多接口,而这些接口就是实现新模式或者新扩展的基石. 在 ...

  6. Unity 解决 An asset is marked with HideFlags.DontSave but is included in the build 问题。

    问题是:不能使用Unity使用的默认的字体.想要显示中文直接去下载一个字体.没必要使用默认的字体

  7. 【Python】Markov text generator马尔科夫文字生成器

    遍历一段文字,统计每个字后面出现的字和其次数,当前一个字确定的时候,计算后一个字出现的百分比,用这个百分比作为文字生成器中后一个字出现的概率进行文字生成 from random import rand ...

  8. Python学习笔记——基础篇【第六周】——shutil模块

    常用模块之shutil 高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中,可以部分内容 def c ...

  9. wpf 遍历listview 时 传入指定类型 得到指定类型控件info

    private ChildType FindVisualChild<ChildType>(DependencyObject obj) where ChildType : Dependenc ...

  10. javabean解决jsp中中文乱码问题

    问题描述:useBean行为定义了Java Bean对象(Person类包括姓名[string],年龄[int]),使用html页面向JSP页面提交数据,JSP页面中使用Java Bean行为来处理提 ...