poj 3278 搜索
描述:
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
Line 1: Two space-separated integers: N and K
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample Input
5 17
Sample Output
4
Hint
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
题意:
农民和奶牛各在一个地方,农民可以通过+1,-1,*2来移动,每走一步用时一分钟,问移动到奶牛所在的位置需要多久。
题解:
广搜,分别进行+1,-1,*2操作,存入队列。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue> using namespace std;
queue<int>q;
int a,b;
bool vis[];
int step[]; int bfs()
{
int u,v;
q.push(a);
step[a]=;
vis[a]=true;
while(!q.empty())
{
u=q.front();
q.pop();
for(int i=;i<;i++)
{
if(i==) v=u+;
else if(i==) v=u-;
else v=u*;
if(v>=||v<) continue;
if(!vis[v])
{
step[v]=step[u]+;
vis[v]=true;
q.push(v);
}
if(v==b) return step[v];
}
}
return -;
} int main()
{
while(cin>>a>>b)
{
memset(step,,sizeof(step));
memset(vis,false,sizeof(vis));
while(!q.empty()) q.pop();
if(a>=b) printf("%d\n",a-b);
else
{
int ans=bfs();
cout<<ans<<endl;
}
}
return ;
}
poj 3278 搜索的更多相关文章
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
- 【BFS】POJ 3278
POJ 3278 Catch That Cow 题目:你要去抓一头牛,给出你所在的坐标和牛所在的坐标,移动方式有两种:要么前一步或者后一步,要么移动到现在所在坐标的两倍,两种方式都要花费一分钟,问你最 ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- poj 3278 Catch That Cow(记忆化广度优先搜索)
题意: 0到N的数轴上,每次可以选择移动到x-1,x+1,2*x,问从n移动到k的最少步数. 思路: 同时遍历三种可能并记忆化入队即可. Tips: n大于等于k时最短步数为n-k. 在移动的过程中可 ...
- 搜索 || BFS || POJ 3278 Catch That Cow
农夫在x位置,下一秒可以到x-1, x+1, 2x,问最少多少步可以到k *解法:最少步数bfs 要注意的细节蛮多的,写在注释里了 #include <iostream> #include ...
- kuangbin专题 专题一 简单搜索 Catch That Cow POJ - 3278
题目链接:https://vjudge.net/problem/POJ-3278 题意:人可以左移动一格,右移动一格,或者移动到当前位置两倍下标的格子 思路:把题意的三种情况跑bfs,第一个到达目的地 ...
- Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
随机推荐
- svnsync同步svn
使用svnsync实现已有版本库的镜像svn不支持分布式开发,所以把svn版本库保存在一台服务器上是不安全的.制作一个镜像svn版本库有多种方式,我采用subversion自带的svnsync程序. ...
- web 日历 任务 插件
[2] 日历任务插件(jquery版)新增日历任务和点击监听 - qq_26462567的博客 - CSDN博客https://blog.csdn.net/qq_26462567/article/de ...
- 关于PHP自动捕捉处理错误和异常的尝试
之所以想着做错误和异常的自动处理是因为: 用的公司自己的框架写API,没有异常和错误相关功能, 而每次操作都进行try...catch,有点繁琐不说,感觉还很鸡肋,即使我catch到了,还是得写代码进 ...
- vue非父子组件之间的通信
https://www.cnblogs.com/chengduxiaoc/p/7099552.html //vm.$emit( event, arg ) //触发当前实例上的事件 //vm.$on ...
- kettle表更新/插入更新
更新: 1.1更新表: 目标表: 插入更新: 2.1匹配表: 目标表: 插入/更新转换 目标表
- sql server 一直提示正在还原
restore database 数据库名称 with recovery
- 洛谷P4178 Tree (算竞进阶习题)
点分治 还是一道点分治,和前面那道题不同的是求所有距离小于等于k的点对. 如果只是等于k,我们可以把重心的每个子树分开处理,统计之后再合并,这样可以避免答案重复(也就是再同一个子树中出现路径之和为k的 ...
- 转:centos 7 安装音频视频解码器
(原文:https://blog.csdn.net/zhou1519/article/details/39035233/) 1.安装额外的软件源epel和nux-dextop rpm -Uvh htt ...
- vue---组件间通信
vue中比较重要的就是组件了.而组件随处可复用的特性,使得组件通信非常重要.那么组件之间通讯方式有哪些呢? 第一种:父子组件通讯: 如果是 html页面 中全局注册的组件 和 实例中局部注册的组件 H ...
- GIT-常规操作
本地安装git, 安装文件: Git客户端: 可百度搜索:GIT64位或GIT32位等关键字找到相应的版本进行下载. 本地地址:D:\20-git\Git-2.20.1-64-bit.exe 也可百度 ...