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 ...
随机推荐
- php str_split()函数 语法
php str_split()函数 语法 str_split()函数怎么用 php str_split()函数用于把字符串分割到数组中,语法是str_split(string,length),将字符串 ...
- LDD3 第7章 Time,Delays and Deferred Work
处理时间委托包括如下任务,按复杂度依次上升: 测量时间流失和比较时间 知道当前时间 指定时间量的延时操作 调度异步函数在之后的时间发生 一.测量时间流失 系统定时硬件规律的产生定时器中断,在内核启动阶 ...
- BZOJ 2281: [Sdoi2011]黑白棋(dp+博弈论)
传送门 解题思路 首先发现可以把相邻的黑白棋子之间的距离看成一堆棋子,那么这个就可以抽象成\(Nim\)游戏每次可以取\(d\)堆这个游戏,而这个游戏的\(SG\)值为\(x\%(d+1)\),那么题 ...
- 【靶场练习_sqli-labs】SQLi-LABS Page-4 (Challenges)
Less-54: ?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where tabl ...
- 【靶场练习_sqli-labs】SQLi-LABS Page-2 (Adv Injections)
Less-21:括号+单引号绕过+base64cookie编码 总感觉我已经把sql注入做成代码审计了:P <?php //including the Mysql connect paramet ...
- Linux 虚拟机通过NAT模式访问外网
1.配置本地VM8地址 2.配置虚拟机NAT网卡:设置VM8网卡地址和Linux主机相同网段地址,网关随便设置 3.编译网卡文件 vim /etc/sysconfig/network-scri ...
- 2018-2019-2 20175203 实验四《Android 开发基础》
20175203 2018-2019 实验四<Android 开发基础> 实验要求 参考Android开发简易教程 完成云班课中的检查点,也可以先完成实验报告,直接提交.注意不能只有截图, ...
- B-Tree, B+Tree, B*树介绍
[数据结构]B-Tree, B+Tree, B*树介绍 转 [数据结构]B-Tree, B+Tree, B*树介绍 [摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是 ...
- VB.NET导出Excel 轻松实现Excel的服务器与客户端交换 服务器不安装Office
说来VB.Net这个也是之前的一个项目中用到的.今天拿来总结下用途,项目需求,不让在服务器安装Office办公软件.这个也是煞费了一顿. 主要的思路就是 在导出的时候,利用DataTable做中间变量 ...
- 配置github的pull request触发jenkins自动构建
参照: https://www.cnblogs.com/zanjiahaoge666/p/6402738.html 之前的配置,都是向master分支push操作触发jenkins进行构建,但是在一般 ...