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 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.
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 ...
随机推荐
- Separate String
Separate String 时间限制: 1 Sec 内存限制: 128 MB提交: 50 解决: 16 题目描述 You are given a string t and a set S of ...
- Struts2 流程原理
一.流程图 (转) 二.流程详解 1.服务器传递来的请求,通过ActionContextClearUp.other filters.最后到达StrutsPrepareAndExecuteFilter ...
- C#选择文件保存路劲
private void button8_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrow ...
- 浅读vuex源码,了解vuex基本原理
极简版vuex代码 class KVuex { constructor (options) { this.state = options.state this.mutations = options. ...
- 什么是Web和www
什么是Web和www 通过之前课程的学习,我们已经对计算机网络有了一些了解,这里我主要想说一个点,也是计算机网络中一个很容易被误解的概念,就是什么是Web,它和HTTP.HTML.Internet.i ...
- gcc/g++ 以及makefile
生成可执行文件 g++ mutiprocess.cpp -o test -fpic:产生位置无关码,位置无关码就是可以在进程的任意内存位置执行的目标码,动态链接库必须使用 -c : 只生成 .o ...
- 深度:Hadoop对Spark五大维度正面比拼!
每年,市场上都会出现种种不同的数据管理规模.类型与速度表现的分布式系统.在这些系统中,Spark和hadoop是获得最大关注的两个.然而该怎么判断哪一款适合你? 如果想批处理流量数据,并将其导入HDF ...
- 仍然有人在叫喊C语言已经过时了
现在,仍然有人在叫喊C语言已经过时了.还有什么值得学习的?看看现在使用Python.PHP和其他语言有多简单.谁去学旧的C语言?是真的吗?作者下载了这两种语言的底层源代码.由于空间的限制,它没有分析框 ...
- flex整页布局
使用flex进行整页的三列布局,flex:1下的子元素无法铺满父级.给flex:1元素,添加stretch拉伸 display: flex; align-content: stretch; align ...
- SeekBar 滚动条
原seek_thumb样式----------------------------------------------------------------------↑↑↑↑↑ android:thu ...