#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. day10 文件处理指针使用 函数基本使用

    一:文件指针 强调:只有t模式下read(n),n代表字符个数,除此之外都是以字节为单位 with open('a.txt',mode='rt',encoding='utf-8') as f: #文本 ...

  2. 【scrapy】关于爬取的内容是Unicode编码

    自己练习爬取拉钩网信息的时候爬取的信息如下: {'jobClass': [u'\u9500\u552e\u52a9\u7406'], 'jobUrl': u'https://www.lagou.com ...

  3. Kubernetes 之上的架构应用

    规划并运转一个兼顾可扩展性.可移植性和健壮性的运用是一件很有应战的事情,尤其是当体系杂乱度在不断增长时.运用或体系 本身的架构极大的影响着其运转办法.对环境的依靠性,以及与相关组件的耦合强弱.当运用在 ...

  4. docker使用以及dockerfile编写

    一 docker常用命令 1. service docker start 2. docker images        显示所有镜像 3. docker ps [-a]          显示正在运 ...

  5. verilog task1

    问题描述: 设计中需要重复多次施加一种激励,每一次激励的施加过程,都可以划分为4个部分,如图所示. 每一次施加的激励只有第二部分的数据有变化(数据格式无变化).所以顶层的Testbench代码如下: ...

  6. layer数据表格换行

    在使用layer数据表格的时候,默认是不可以换行的.这样显示 改动后 数据格式为   aa<br>bb就会显示为换行 比如我们的字符串是    a<br>b 这样的字符串浏览器 ...

  7. Python+Selenium学习--分页处理

    场景 我们在测试一个web 应用时,经常出现翻页的情况,下面介绍翻页场景 代码 #!/usr/bin/env python # -*- codinfg:utf-8 -*- ''' @author: J ...

  8. C++旅馆问题。

    有总钱数 有每房每天住需要多少钱 问最少可以住几天 最后输入的是钱数.前边输入没个住所每天多少钱 例如: 1001 1002 1003 1004 1000 -1 100 500 600 最少一天,最多 ...

  9. js封装插件

    js方式: (function(){ var demo = function(options){ this.options = $.extend({ "x" : "1&q ...

  10. SprinMVC中文件上传只在内存保留一份拷贝

    背景:web项目里经常有上传文件的模块,某些特殊场景下,上传文件的人不希望在服务器留存一份原始文件,这个时候就需要把文件放到内存里了. 笔者调试了一下springmvc里面的CommonsMultip ...