【搜索】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 ...
随机推荐
- python全栈 流程控制;while 循环 格式化输出 运算符 及编码
python全栈开发 1循环 2break和continue的区别 3格式化输出 4运算符 5编码 一.流程控制while循环 while条件: 代码块(循环体) 1.死循环; while True; ...
- django1.10使用本地静态文件
django1.10使用本地静态文件方法 本文介绍的静态文件使用,是指启动web站点后,访问静态资源的用法,实际静态资源地址就是一个个的url 如果没有启动web站点,只是本地调试html页面,那直接 ...
- modal template
<div class="modal fade" id="tmp_order_modal" tabindex="-1" role=&qu ...
- 强制改变css样式优先级
.list{ left:20px!important; } css !important作用是提高指定CSS样式规则的应用优先权. !important是CSS1就定义的语法,作用是提高指定样式规则的 ...
- PHP之PDO
PHP之PDO介绍 PDO为何物? POD(PHP Data Object)扩展在PHP5中加入,PHP6中将默认识用PDO连接数据库,所有非PDO扩展将会在PHP6被从扩展中移除.该扩展提供PHP内 ...
- f5双机配置
1.1)要确认2 台设备的型号及版本完全一致! 2)2台设备分别取名f5_4.com 和f5_3.com,并将时区修改成上海时区 注:设备的hostname不能随便修改. 3)VLAN配置 由于当前环 ...
- ubuntu下安装redis及常用操作
reids是一个内存数据库,具有存取速度快,使用简单等优点.我们常常在分布式或者负载均衡的系统中使用它来缓存一些公用的且不是很大量的数据,比如session或者各类token(比如微信的access_ ...
- 本地推送UILocalNotification的一些简单方法
1.添加本地推送,需要在app添加推送.可以根据通知的userInfo的不同的键值对来区分不同的通知 UILocalNotification *notification = [[UILocalNoti ...
- MVc Identity登陆锁定
2016-08-03 [ASP.NET Identity] OAuth Server 鎖定(Lockout)登入失敗次數太多的帳號 743 6 ASP.NET Identity 檢舉文章 2016-0 ...
- DevExpress之TreeList节点绑定图片
最近在项目中使用到了DX中的TreeList控件绑定数据源时在每个节点前显示特点的图片:查阅相关资料实现方法如下:1.首先打开VS2010新建一个WINFROM应用程序: 2.在WINFROM应用程序 ...