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 ...
随机推荐
- pb datawindow 类型
DataWindow.Processing 判断 DataWindow 对象的类型 可用 DataWindow.Processing 判断 DataWindow 对象的类型,dw的类型如下: ...
- Java 集合和泛型
一.集合(Collections) Java使用集合来组织和管理对象. 1.Java的集合类 集合类主要负责保存.盛装和管理对象,因此集合类也被称为容器类. 集合类分为Set.List.Map和Que ...
- 1-Perl 简介
Perl 是 Practical Extraction and Report Language 的缩写,可翻译为 "实用报表提取语言".Perl 是高级.通用.直译式.动态的程序语 ...
- C语言写郑州大学校友通讯录
#include <stdio.h> #include <string.h> #include <stdlib.h> #define LEN sizeof(stru ...
- [C#]使用BackgroudWorker刷新UI延迟的解决方法
今天使用BackgroundWorker刷新UI发生延时现象,找了好久才发现AutoResetEvent可以解决,代码如下 private void BgWorker_ProgressChanged( ...
- 【Git的基本操作五】比较文件差异
比较文件差异 1. git diff [文件名] 将工作区中的文件和暂存区对应文件进行比较 例:git diff test.txt 2. git diff [本地库中文件历史记录(指针)] [文件名] ...
- vue 登录 + 记住密码 + 密码加密解密
<template> <el-form :model="ruleForm"> <h3 class="title">系统登录& ...
- JPanel实现滚动条
之前一直用JScrollPane里面放一个JTextArea,就可以在文本框内实现滚动条. 但是最近做一个小demo,需要在JPanel中实现滚动条,就找了下资料,做好了,现在记录一下,防止以后再用到 ...
- 用vue写一个分页器代码
分页是项目中常会用到的,网上的插件也很多,但是有些功能太齐全反而不是必要的,所以抽时间自己写了一下(小白写代码,若发现问题还请及时赐教,感激不尽……) 如图,想要一个这样的页码: a. 上一页.下一页 ...
- Linux 永久挂载镜像文件和制作yum源
Linux mount命令是经常会使用到的命令,它用于挂载Linux系统外的文件. 1.镜像挂载到系统指定目录下:[root@master cdrom]# mount -t auto /mnt/c ...