Catch That Cow (POJ - 3278)(简单BFS)
转载请注明出处:https://blog.csdn.net/Mercury_Lc/article/details/82693928作者:Mercury_Lc
题解:给你x、y,x可以加1、减1、或者变成2*x,问通过最少的次数来让x等于y,这是最基础的bfs,就是把x通过一次的+1、-1、*2得到的数都放到队列里面,再把这些通过一次操作得到的数进行相同的操作+1、-1、*2,因为用个结构体来存放这个数是第几次操作得到的,所以只要一旦发现这个数,一定是通过最小的次数得到的。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int maxn = 1e6 + 10;
int vis[maxn];
struct node
{
int data;
int step;
} w,l;
void bfs(int n,int k)
{
memset(vis,0,sizeof(vis));
vis[n] = 1;
queue<node>q;
w.data = n;
w.step = 0;
q.push(w);
while(!q.empty())
{
w = q.front();
q.pop();
if(w.data == k)
{
printf("%d\n",w.step);
return ;
}
if(w.data + 1 <= maxn && !vis[w.data + 1])
{
l = w;
l.data += 1;
l.step ++;
q.push(l);
vis[l.data] = 1;
}
if(w.data - 1 <= maxn && w.data - 1 >= 0&& !vis[w.data - 1])
{
l = w;
l.data -= 1;
l.step++;
q.push(l);
vis[l.data] = 1;
}
if(w.data * 2 <= maxn && !vis[w.data * 2])
{
l =w;
l.step++;
l.data *= 2;
q.push(l);
vis[l.data] = 1;
}
}
return ;
}
int main()
{
int n,k;
while(~scanf("%d %d",&n,&k))
{
if(n > k)
printf("%d\n",n - k);
else
bfs(n, k);
}
return 0;
}
Problem
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 17Sample Output
4Hint
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.
Catch That Cow (POJ - 3278)(简单BFS)的更多相关文章
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
- Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
- Catch That Cow POJ - 3278 bfs map超时,短路判断顺序。
题意:可以把n边为n+1,n-1,n*2问从n到k的最少变化次数. 坑:标题写了.有点不会写bfs了... ac代码 #define _CRT_SECURE_NO_WARNINGS #include& ...
- kuangbin专题 专题一 简单搜索 Catch That Cow POJ - 3278
题目链接:https://vjudge.net/problem/POJ-3278 题意:人可以左移动一格,右移动一格,或者移动到当前位置两倍下标的格子 思路:把题意的三种情况跑bfs,第一个到达目的地 ...
- (广搜)Catch That Cow -- poj -- 3278
链接: http://poj.org/problem?id=3278 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6211 ...
- poj 3278 简单BFS
题意:给定农夫和奶牛的初始位置,农夫可以当前位置+1.-1.*2三种移动方式,问最少需要多少分钟抓住奶牛 AC代码: #include<cstdio> #include<cstrin ...
- C - Catch That Cow POJ - 3278
//标准bfs #include <iostream> #include <cstdio> #include <algorithm> #include <cm ...
- 牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)
链接:https://ac.nowcoder.com/acm/contest/984/L 来源:牛客网 Catch That Cow 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 3 ...
- poj 3414(简单bfs)
题目链接:http://poj.org/problem?id=3414 思路:bfs简单应用,增对瓶A或者瓶B进行分析就可以了,一共6种状态. #include<iostream> #in ...
- POJ 1101 简单BFS+题意
The Game 题意: Description One morning, you wake up and think: "I am such a good programmer. Why ...
随机推荐
- 怎样使用构造函数: Vue()?
1. 新建一个 .html 文件 => 引入一个在线的 vue 库 => 写一个带 id 的 html 标签 => 写一个 script 标签, 这里的 vApp 是 Vue() 这 ...
- webpack权限控制
const type= "a"; const menusConfig = { a: ["activity"], b: ["activity" ...
- Atcoder-SoundHound Inc.Contest 2018 -Masters Tournament-比赛报告
A C++ Example #include <iostream> #include <cstdio> #include <cstdlib> #include &l ...
- 分布式缓存Redis+Memcached经典面试题和答案
Redis相比memcached有哪些优势? (1) memcached所有的值均是简单的字符串,redis作为其替代者,支持更为丰富的数据类型 (2) redis的速度比memcached快很多 ( ...
- oracle中行转列操作
数据准备阶段: CREATE TABLE CC (Student NVARCHAR2(2),Course NVARCHAR2(2),Score INT); INSERT into CC sele ...
- 安装openblas库
http://www.openblas.net/ Linux:下载源码直接make即可
- GPS坐标转大地坐标
根据网上EXCEL表格给出的关系,生成的C语言代码.计算结果和软件[万能坐标转换980]计算出的结果很接近. double B = 39.3926; double L = 117.4514; //do ...
- C库函数——字符串转数字整理
atof(将字符串转换成浮点型数)atoi(将字符串转换成整型数)atol(将字符串转换成长整型数)strtod(将字符串转换成浮点数)strtol(将字符串转换成长整型数)strtoul(将字符串转 ...
- MySQL单表查询实例
数据表准备 ```mysql create table emp( id int not null unique auto_increment, name varchar(20) not null, ...
- ListView 一维排布 动态滑动添加新item代码
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV ...