POJ 3278 Catch That Cow (有思路有细节的bfs)
Description
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
N and
K
Output
Sample Input
5 17
Sample Output
4 别看题简单,满满的坑啊!!你初始在n,先让你移动到k,有三种移动,—1,+1,*2。问你几步能到k。
最开始无脑搜,直接T了。后来发现bfs虽然是有点暴力的感觉但是还是要有思维在里面的!如果n>k,n只能一点点减到k。还有数组开大点,以防*2后越界。
代码如下:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
int n,k,ans;
bool vis[];
struct node
{
int pos,step;
};
bool check (int x)
{
if (vis[x])
return ;
if (x<||x>)
return ;
else
return ;
}
void bfs (node now)
{ queue<node>q;
q.push(now);
while (!q.empty())
{
node temp=q.front();
q.pop();
if (check(temp.pos-))
{
node x;
x.pos=temp.pos-;
x.step=temp.step+;
vis[x.pos]=;
if (x.pos==k)
{
ans=x.step;
return ;
}
q.push(x);
}
if (check(temp.pos+)&&temp.pos<k)
{
node x;
x.pos=temp.pos+;
x.step=temp.step+;
vis[x.pos]=;
if (x.pos==k)
{
ans=x.step;
return ;
}
q.push(x);
}
if (check(temp.pos*)&&temp.pos<k)
{
node x;
x.pos=temp.pos*;
x.step=temp.step+;
vis[x.pos]=;
if (x.pos==k)
{
ans=x.step;
return ;
}
q.push(x);
}
}
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d%d",&n,&k))
{
memset(vis,false,sizeof vis);
node now;
now.pos=n;
now.step=;
ans=;
if (n>=k)
printf("%d\n",n-k);
else
{
bfs(now);
printf("%d\n",ans);
}
}
return ;
}
POJ 3278 Catch That Cow (有思路有细节的bfs)的更多相关文章
- 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 (附有Runtime Error和Wrong Answer的常见原因)
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- 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(求助大佬)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 109702 Accepted: 34255 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
随机推荐
- JS中关于构造函数、原型链、prototype、constructor、instanceof、__proto__属性
在Javascript不存在类(Class)的概念,javascript中不是基于类的,而是通过构造函数(constructor)和原型链(prototype chains)实现的.但是在ES6中引入 ...
- python中%代表什么意思?
http://zhidao.baidu.com/link?url=MQLeRPckNfavTJYvMQbVj_pdNn5SSadtFvfEk7nNCusPcPW4T1O45esIuttuBW3EnSB ...
- Hibernate中常用HQL
HQL是Hibernate自带的查询语言 HQL是一种面向对象的查询语言.SQL的操作对象是数据表和列等数据对象,而HQL的操作对象是类.实例.属性等. HQL的语法很像SQL的语法 以下举例均以学生 ...
- ActiveReports报表数据源:运行时数据源
ActiveReports 支持在设计时和运行时设置报表数据源,本文将重点介绍如何在运行时为区域报表和RDL/页面报表设置报表数据源. ·第一步: 在工程中创建一个名为 SectionReport_D ...
- python3+django2 个人简单博客实现 -正在施工
- 关于toString()的一些事情
Java上输出一个数组的时候,不可以直接输出 System.out.println(arr); 直接输出数据的名称会输出数组的内存地址.换句话说,他的输出是: getClass().getName() ...
- DNS 放大
DNS放大攻击是伪造一个DNS查询的报文,源地址改成想要攻击的IP.单个查询的包64字节,如果是ANY类型查询(或者DNSSEC记录),那么回复报文一般会大几十倍.当然,如果攻击者自己制造一个很大的T ...
- FastReport.net 使用 Winform WebForm打印
delphi用的fastreport比较多 所以.net中也研究一下用法,这个打印控件还是很简单的 只要手动设计一下写少许代码就可以打印了 甚至可以写成通用代码 以后就可以不用写代码 安装demo会同 ...
- matplotlib系列——条形图
import matplotlib.pyplot as plt import numpy as np import matplotlib x = ["战狼2","激情8& ...
- cygwin的用途
cgywin简介 打开cygwin的官方网站(www.cygwin.com),一行英文非常醒目:Get that Linux feeling – on Windows! 简而言之,cygwin是一个在 ...