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. ...
随机推荐
- c++ fmt 库安装和使用示例
安装: 1 git clone https://github.com/fmtlib/fmt.git 2. cmake . 3. make && make install #incl ...
- 基于mybatis基本操作
实体 在对上述三个实体操作中了解mybatis的实现 全部代码 pro1是另外的一个完整项目用spring和struts2 hibernate 下载 https://github.com/Danov ...
- JQ面向对象的放大镜
index.html <!DOCTYPE html><html> <head> <meta charset="utf-8" /> & ...
- Linux C/C++ 链接选项之静态库--whole-archive,--no-whole-archive和--start-group, --end-group
参照这两篇博客: http://stackoverflow.com/questions/805555/ld-linker-question-the-whole-archive-option http: ...
- Lodop打印表格带页头页尾 自动分页每页显示头尾
Lodop中有两种专门给超文本表格的方式,ADD_PRINT_TABLE和ADD_PRINT_TBURL,该方式只能用于单个表格,表格外的内容不显示,是专门用于打印html超文本表格的.使用这两个语句 ...
- kuangbin最小生成树专题
网址:https://vjudge.net/contest/66965#overview 第一题: poj1251 裸最小生成树 #include<iostream> #include&l ...
- 【数学建模】MATLAB语法
一.向量.矩阵的表示和使用 format long %小数很多format short %默认4位小数format rat %显示最近的分数format short e %指数格式的数 尾数多少 e ...
- mysql主从复制、redis基础、持久化和主从复制
一.mysql(mariadb)基础 1.基础命令(centos7操作系统下) 1.启动mysql systemctl start mariadb 2.linux客户端连接自己 mysql -uroo ...
- Codeforces 1077D Cutting Out(二分答案)
题目链接:Cutting Out 题意:给定一个n长度的数字序列s,要求得到一个k长度的数字序列t,每次从s序列中删掉完整的序列t,求出能删次数最多的那个数字序列t. 题解:数字序列s先转换成不重复的 ...
- 使用Flink实现索引数据到Elasticsearch
使用Flink实现索引数据到Elasticsearch 2018-07-28 23:16:36 Yanjun 使用Flink处理数据时,可以基于Flink提供的批式处理(Batch Proce ...