超超超简单的bfs——POJ-3278
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 89836 | Accepted: 28175 |
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Output
Sample Input
5 17
Sample Output
4
Hint
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int a[];//一个2倍大小的数组代表可以走到的位置,因为有乘二所以要开二倍以防RE,a[i]=x --> 走到坐标i需要x步
int main()
{
int n, k, t;
queue<int>q;
scanf("%d%d", &n, &k);
memset(a, -, sizeof(a));//所有坐标初始化为-1
a[n] = ; //n走到n当然是需要步
q.push(n); //当前位点入队
while (!q.empty())
{
t = q.front(); //读取队首
q.pop(); //删除队首
if (t == k) //若到达终点则直接输出并结束
{
printf("%d\n", a[k]);
return ;
}
if (t - >= && a[t - ] == -)//可能到达的结点入队,要判断是否越界,走过的不再走
{
a[t - ] = a[t] + ; q.push(t - );//下一步走到的位点所需步数是当前位点的步数+1
}
if (t + < && a[t + ] == -)
{
q.push(t + ); a[t + ] = a[t] + ;
}
if (t * < && a[t * ] == -)
{
q.push(t * ); a[t * ] = a[t] + ;
}
}
}
简单的bfs
超超超简单的bfs——POJ-3278的更多相关文章
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)
BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...
- 搜索 || BFS || POJ 3278 Catch That Cow
农夫在x位置,下一秒可以到x-1, x+1, 2x,问最少多少步可以到k *解法:最少步数bfs 要注意的细节蛮多的,写在注释里了 #include <iostream> #include ...
- 转帖: 一份超全超详细的 ADB 用法大全
增加一句 连接 网易mumu模拟器的方法 adb connect 127.0.0.1:7555 一份超全超详细的 ADB 用法大全 2016年08月28日 10:49:41 阅读数:35890 原文 ...
- 【BFS】POJ 3278
POJ 3278 Catch That Cow 题目:你要去抓一头牛,给出你所在的坐标和牛所在的坐标,移动方式有两种:要么前一步或者后一步,要么移动到现在所在坐标的两倍,两种方式都要花费一分钟,问你最 ...
- 超全超详细的HTTP状态码大全(推荐抓包工具HTTP Analyzer V6.5.3)
超全超详细的HTTP状态码大全 本部分余下的内容会详细地介绍 HTTP 1.1中的状态码.这些状态码被分为五大类: 100-199 用于指定客户端应相应的某些动作. 200-299 用于表示请求成功. ...
- 搜索入门_简单搜索bfs dfs大杂烩
dfs题大杂烩 棋盘问题 POJ - 1321 和经典的八皇后问题一样. 给你一个棋盘,只有#区域可以放棋子,同时同一行和同一列只能有一个棋子. 问你放k个棋子有多少种方案. 很明显,这是搜索题. ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
随机推荐
- abelkhan服务器框架
abelkhan是一个开源的游戏服务器框架.目标是提供一个稳定.高效.可扩展的服务器框架. github:https://github.com/qianqians/abelkhan 论坛:http:/ ...
- 使用flask开发网站后端
Flask 是一个用于 Python 的微型网络开发框架,可以用于快速的搭建一个小型的网站. 我的搜索引擎:http://www.abelkhan.com 就是基于flask开发 一个flask的He ...
- 增广拉格朗日乘子法(Augmented Lagrange Method)
转载自:增广拉格朗日乘子法(Augmented Lagrange Method) 增广拉格朗日乘子法的作用是用来解决等式约束下的优化问题, 假定需要求解的问题如下: minimize f(X) s.t ...
- Python读入CIFAR-10数据库
CIFAR-10可以去http://www.cs.toronto.edu/~kriz/cifar.html下载(记得下载python格式) CIFAR-10数据组成: 训练集和测试集分别有50000和 ...
- 12.js如何将明文转为MD5
1.先下载MD5.JS 2.引入,使用hex_md5(str)即可
- 关于 静态页面布局 中的一些BUG
作为一枚初级程序猿,难免在制作静态页面时会遇到一些BUG,在此,我从网上找了一些资料并且结合自己的项目开发经验,总结了一些在静态页面布局时可能会遇到的问题,希望能对初级程序猿有一定的帮助(资料请参考: ...
- database.properties数据源
jdbc.driver_class=oracle.jdbc.driver.OracleDriverjdbc.connection.url=jdbc:oracle:thin:@localhost:152 ...
- CentOS7 下使用YUM安装 MySQL5.7
于2015年10月19日(美国时间),Oracle公司发布了开源数据库MySQL的最新版本5.7.到现在已有将近3年之久,经过这几年的改进,MySQL5.7性能最高可达前一个版本的3倍,现在官网的最新 ...
- log 的 debug()、 error()、 info()方法的区别
软件中总免不了要使用诸如 Log4net, Log4j, Tracer 等东东来写日志,不管用什么,这些东东大多是大同小异的,一般都提供了这样5个日志级别: × Debug × Info ...
- 基于JQ的单双日历,本人自己写的哈,还没封装,但是也能用
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>小 ...