【搜索】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 ...
随机推荐
- 一个范围的两个数进行数位的累加,实现对两个数num1和num2的数位和相加
对一个范围的两个数进行数位的累加,例如有两个数 15,19则 他们的数位和应该为: 1+5+1+6+1+7+1+8+1+9,结果为40. 测试说明 样例1 输入:1519 输出: 40 以下是不同方法 ...
- CentOS 安装Oracle
转自----------------https://www.cnblogs.com/startnow/p/7580865.html 环境:VM12+centos7 x86_64 minimal - 最 ...
- use crunch compression
Crunch is a lossy compression format on top of DXTR texture compression. Textures will be converted ...
- jmeter简单压测设置
参数化 随机参数 时间参数 顺序自增函数 文件读取 直接引用 响应断言 用来查看sessionid 关联 关联引用 jmeter操作数据库 安装连接程序包 ip 端口号 哪个数据库 可以执行多条s ...
- sqlserver判断字段是否存在更改字段
use naire go if COL_LENGTH('options','optionsGroup') is null begin--options为表名,optionsGroup为列名 alter ...
- AngularJS——第4章 数据绑定
第4章 数据绑定 AngularJS是以数据做为驱动的MVC框架,所有模型(Model)里的数据经由控制器(Controller)展示到视图(View)中. 所谓数据绑定指的就是将模型(Model)中 ...
- python读写剪贴板
#coding:utf-8 import os import time import win32api import win32con import win32clipboard as w impor ...
- mysql定位慢查询
mysql定位慢查询 //显示数据库的状态 show status; //显示执行了多少次插入 show status like 'com_insert'; //显示执行了多少次更新 show sta ...
- HTML day48
前端知识之HTML内容 HTML介绍 Web服务本质 import socket#引入套接字模块 sk = socket.socket()#实例化一个套接字对象 sk.bind(("12 ...
- c#一个统计运行时间方法
public string STD(int HowManySecond) { ) { "; } string ShowStr = ""; * )) { ShowStr + ...