POJ 3278 Catch That Cow(模板——BFS)
题目链接:
http://poj.org/problem?id=3278
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
Output
Sample Input
5 17
Sample Output
4
Hint
输入人和牛在坐标轴上的位置
#include<stdio.h> int bfs(int n,int k);
struct node
{
int x,s;
};
struct node q[];
int book[];//标记数组,否则超内存 int main()
{
int n,k;
while(scanf("%d%d",&n,&k) != EOF)
{
if(n==k)
printf("0\n");
else
printf("%d\n",bfs(n,k));
}
return ;
}
int bfs(int n,int k)
{
int i,head,tail,tx;
head=;
tail=;
q[tail].x=n;
q[tail].s=;
book[n]=;
tail++; while(head < tail)
{
for(i=;i<=;i++)
{
if(i==)
tx=q[head].x-;
if(i==)
tx=q[head].x+;
if(i==)
tx=q[head].x*; if(tx < || tx > )
continue;
if(!book[tx])
{
book[tx]=;
q[tail].x=tx;
q[tail].s=q[head].s+;
tail++; if(tx == k)
return q[tail-].s;
}
}
head++;
}
}
POJ 3278 Catch That Cow(模板——BFS)的更多相关文章
- 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 (bfs)
题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<s ...
- POJ 3278 Catch That Cow(简单BFS)
题目链接:http://poj.org/problem?id=3278 题目大意:给你两个数字n,k.可以对n执行操作(n+1,n-1,n*2),问最少需要几次操作使n变成k. 解题思路:bfs,每次 ...
- POJ - 3278 Catch That Cow 【BFS】
题目链接 http://poj.org/problem?id=3278 题意 给出两个数字 N K 每次 都可以用三个操作 + 1 - 1 * 2 求 最少的操作次数 使得 N 变成 K 思路 BFS ...
- POJ 3278 Catch That Cow(BFS 剪枝)
题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出 ...
- POJ 3278 Catch That Cow【BFS】
题意:给出n,k,其中n可以加1,可以减1,可以乘以2,问至少通过多少次变化使其变成k 可以先画出样例的部分状态空间树 可以知道搜索到的深度即为所需要的最小的变化次数 下面是学习的代码----@_@ ...
- POJ——3278 Catch That Cow(BFS队列)
相比于POJ2251的三维BFS,这道题做法思路完全相同且过程更加简单,也不需要用结构体,check只要判断vis和左右边界的越界情况就OK. 记得清空队列,其他没什么好说的. #include< ...
- 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[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
随机推荐
- [知了堂学习笔记]_用JS制作《飞机大作战》游戏_第2讲(四大界面之间的跳转与玩家飞机的移动)
一.通过点击按钮事件,实现四大界面之间的跳转: (一)跳转的思路: 1.打开软件,只显示登录界面(隐藏游戏界面.暂停界面.玩家死亡界面) 2.点击微信登录(QQ登录)跳转到游戏界面,隐藏登录界面 3. ...
- 使用line-height来垂直居中在安卓设备并不居中,利用ji调整
先判断智能机浏览器的版本信息:方法有两种,但是第一种兼容性更好,但是使用了device插件 第一种: if (device.android()){ } 第二种: var ua = navigator. ...
- 最短的IE判断var ie=!-[1,]分析
以前最短的IE判定借助于IE不支持垂直制表符的特性搞出来的. 复制代码代码如下: var ie = !+"\v1"; 仅仅需要7bytes!参见这篇文章,<32 byte ...
- 讲述Sagit.Framework解决:双向引用导致的IOS内存泄漏(中)- IOS不为人知的Bug
前言: 话说昨晚还是前晚,写了一篇:讲述Sagit.Framework解决:双向引用导致的IOS内存泄漏(上) 文章写到最后时,多了很多莫名奇妙的问题!!! 为了解决了这些莫名奇妙的问题,我又战斗了2 ...
- volatile关键字是如何起作用的?
关键字volatile是Java虚拟机提供的最轻量级的同步机制,但是在平时的项目里面,遇到需要多线程的时候更多地使用的是synchronized关键字来进行同步.个人而言,更多的原因是对volatil ...
- sql server 提示无法彻底删除_复制-而无法删除数据库或重新配置发布订阅
EXEC sp_removedbreplication 'Sys' --记着把当前执行EXEC sp_removedbreplication 'Sys'连接也关闭哦! 即使勾下面关闭连接,还会报错! ...
- Geohash-》基本使用
我把类文件放到了以上这个路径,在要使用的文件引入使用. 以下是测试代码, 1.先实例化类 2.再调用函数 3.这个函数返回GeoHash编码
- 安装好的虚拟机,外部通过ssh工具连接,报connection failed
今天,新装了一台ubuntu虚拟机,安装成功以后,准备利用Xshell从外部访问linux,以减少切换,但是,在连接时,总是会报:connection failed. 于是,写下这篇随笔,以增加记忆且 ...
- Java学习笔记6(循环和数组练习题)
1.输出100到1000的水仙花数: public class LoopTest{ public static void main(String[] args){ int bai = 0; int s ...
- console报错类型
常见 console报错 Error 错误 EvalError 全局错误 RangeError 引用(范围)错误 ReferenceError 参数(参考)错误 SyntaxError 语法错误 ty ...