#include<stdio.h>
#include<string.h>
struct A{
int state;
int step;
}queue[]; // 结构体数组用来模拟队列,数组元素包含两个数据,state代表遍历到的数值,step所经历的步数
int vis[]; // 这个数组用来存储访问情况
int n, k;
int bfs( int aim);
int main(void){ scanf("%d%d", &n, &k);
if( n >= k) printf("%d\n", n-k); // 如上图可知,n>=k的最优解一定是每一步都向后退
else printf("%d\n", bfs(n));
return ;
}
int bfs( int aim){
struct A temp; // 结构体元素用来当做循环单位
int head, rear; // 队首队尾指针
memset( vis, , sizeof(vis));
vis[aim] = ;
head = rear = ;
queue[rear].state = aim; // 起点作为第0个元素入队
queue[rear++].step = ; // 此时也是第0步,然后队尾指针向后移动 while( head < rear){ // 队空时结束循环
temp = queue[head++]; // 队首元素出队
// 第一种操作
if( temp.state+ > && temp.state+ <= && !vis[temp.state+]){
queue[rear].state = temp.state + ; // 操作后的元素入队,记录数值以及步数
queue[rear++].step = temp.step + ;
vis[temp.step+] = ; // 此时标记访问
if( temp.state + == k) return temp.step + ; // 如果和目标相等则返回步数
}
// 第二种操作
if( temp.state- > && temp.state- <= && !vis[temp.state-]){
queue[rear].state = temp.state - ;
queue[rear++].step = temp.step + ;
vis[ temp.state-] = ;
if( temp.state- == k) return temp.step + ;
}
// 第三种操作
if( temp.state* > && temp.state* <= && !vis[temp.state*]){
queue[rear].state = temp.state * ;
queue[rear++].step = temp.step + ;
vis[ temp.state*] = ;
if( temp.state* == k) return temp.step + ;
}
}
return ;

本题使用DFS搜索对当前点进行N*2 N+1 N-1三种操作进行搜索

【搜索】C - Catch That Cow的更多相关文章

  1. [kuangbin带你飞]专题一 简单搜索 - C - Catch That Cow

    #include<iostream> #include<cstdio> #include<string> #include<vector> #inclu ...

  2. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

  3. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. catch that cow POJ 3278 搜索

    catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...

  5. catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38263   Accepted: 11891 ...

  6. Catch That Cow(广度优先搜索_bfs)

     Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 48036   Accepted: 150 ...

  7. 2016HUAS暑假集训训练题 B - Catch That Cow

    B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...

  8. HDU 2717 Catch That Cow (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...

  9. Catch That Cow 分类: POJ 2015-06-29 19:06 10人阅读 评论(0) 收藏

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 58072   Accepted: 18061 ...

  10. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

随机推荐

  1. PAT1018 (dijkstra+dfs)

    There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...

  2. MenOS

    2018-2019-120189224 <庖丁解牛Iinux内核分析>第三周学习总结 MenuOS的构造 下载内核源代码编译内核 cd ~/LinuxKernel/ wget https: ...

  3. C++中 top()与pop()

    top()是取出栈顶元素,不会删掉栈里边的元素 pop()是删除栈顶元素.

  4. Angular之响应式表单 ( Reactive Forms )

    项目结构 一 首页 ( index.html ) <!doctype html> <html lang="en"> <head> <met ...

  5. 超强、超详细Redis入门教程【转】

    这篇文章主要介绍了超强.超详细Redis入门教程,本文详细介绍了Redis数据库各个方面的知识,需要的朋友可以参考下 [本教程目录] 1.redis是什么2.redis的作者何许人也3.谁在使用red ...

  6. jmeter简单压测设置

    参数化 随机参数 时间参数 顺序自增函数  文件读取  直接引用 响应断言 用来查看sessionid 关联 关联引用 jmeter操作数据库 安装连接程序包 ip 端口号 哪个数据库 可以执行多条s ...

  7. 运行wmic命令异常:java.io.IOException: Cannot run program "wmic": CreateProcess error=2, ϵͳÕҲ»µ½ָ¶解决记录

    之前的一篇博文获取电脑cpu序列号在一同事电脑出现上述异常 百度一下网上只有一位外国网友朋友这个问题(地址),并且还没人回复,你能靠自己了 定位了一下出错代码: Process process = R ...

  8. checkbox控制text是否可以填写和radio是否可选

    代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" con ...

  9. IE6、7下overflow:hidden失效的问题

    问题产生原因: 当父元素的直接子元素或者下级子元素的样式拥有position:relative或者position:absolute属性时,父元素的overflow:hidden属性就会失效. 例如: ...

  10. 【APT】SqlServer游标使用

    use [ElephantCredit] go begin transaction tran_bank; print '**脚本开始执行!'; declare @tran_error int , @n ...