【搜索】C - Catch That Cow
#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的更多相关文章
- [kuangbin带你飞]专题一 简单搜索 - C - Catch That Cow
#include<iostream> #include<cstdio> #include<string> #include<vector> #inclu ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
- catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38263 Accepted: 11891 ...
- Catch That Cow(广度优先搜索_bfs)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 48036 Accepted: 150 ...
- 2016HUAS暑假集训训练题 B - Catch That Cow
B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...
- 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 ...
- 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 ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
随机推荐
- Java 管道PipedInputStream PipedOutStream PipedReader PipedWriter
java中的管道流(pipeStream)是一种特殊的流,用于在不同线程间直接传送数据.一个线程发送数据到输出管道,另外一个线程从输入管道中读取数据.通过使用管道,实现不同线程间的通信,而不必借助类似 ...
- php 读取网站页面源码的经典函数
Snoopy.class.php下载 include "inc/Snoopy.class.php"; //读取网页,返回网页源文件内容 function read_url($str ...
- Ant 初级入门
一.Ant简介 Ant是一个Apache基金会下的跨平台的构件工具. 由于Ant是基于Java的,所以需要Java环境的支持,也就是配置好 JAVA_HOME 和 ANT_HOME 环境变量分别指向J ...
- 动态添加select选项空选项问题
问题:动态添加校区选项的数据的时候,总是多添加一项空白的数据. 动态添加代码如下: 网上找到的原因:因为在option中有标签没有闭合,所以导致浏览器认为是两个option, 所以只需要给这个标签添加 ...
- stm32GPIO的8种工作模式
推挽输出:可以输出高.低电平,连接数字器件:推挽结构一般是指两个三极管分别受两个互补信号的控制,总是在一个三极管导通的时候另一个截止.高低电平由IC的电源决定. 推挽电路是两个参数相同的三极管或MOS ...
- UVa 536 Tree Recovery(二叉树后序遍历)
Little Valentine liked playing with binary trees very much. Her favorite game was constructing rando ...
- SQL Server 触发器 表的特定字段(一个字段)更新时,触发Update触发器
CREATE TRIGGER [dbo].[Trg_Update_table1_column1] on table1 after update as if update (column1) ...
- day 18 类,对象
类,对象: 类 具有相似功能的一类事物,人类,犬类,猫类等等. 对象: 类的具体表现 面向对象: 1.第一个优点:面向对象是一类相似功能函数的集合体 更清晰化,更规范化 class LoginHand ...
- day 09 函数的进阶
01 动态参数 *args **kwargs 在函数的定义时,* ** 代表聚合. def func(**kwargs): print(kwargs) func(**{"name" ...
- LWP::UserAgent的用法
LWP::UserAgent是一个模拟用户浏览器的类,在使用的时候需要遵守以下几步: 1.引入模块 2.创建一个LWP::UserAgent的对象 3.设置这个对象的相关参数 4.创建HTTP::Re ...