Catch That Cow(广度优先搜索_bfs)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 48036 | Accepted: 15057 |
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
K
Output
Sample Input
5 17
Sample Output
4
题意:输入两个数n,k。求从n到k最少走多少步。能够前进1后退1或者当前的位置*2。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
struct node
{
int x;//当前位置
int ans;//走的步数
}q[1000010];
int vis[1000010];//标记变量,该点是否被訪问;
int jx[]={-1,1};//后退1或者前进1。
struct node t,f;
int n,k;
void bfs()
{
int i;
int s=0,e=0;//指针模拟队列。 往队列加e++ 往队列里提出数s++
memset(vis,0,sizeof(vis));
t.x=n;//当前初始位置
vis[t.x]=1;//标记为1代表訪问过。
t.ans=0;//初始位置步数为0;
q[e++]=t;//把当前步数加人队列
while(s<e)//当队列不为空
{
t=q[s++];//提出
if(t.x==k)//假设该数正好等于目标位置直接输出步数
{
printf("%d\n",t.ans);
break;
}
for(i=0;i<3;i++)//i=0后退一步,i=1前进一步。i=2此时的位置*2;
{
if(i==2)
{
f.x=t.x*2;
}
else
{
f.x=t.x+jx[i];
}
if(f.x>=0&&f.x<=100000&&!vis[f.x])
{
f.ans=t.ans+1;
q[e++]=f;
vis[f.x]=1;
}
}
}
}
int main()
{
while(~scanf("%d %d",&n,&k))
{
bfs();
}
return 0;
}
Catch That Cow(广度优先搜索_bfs)的更多相关文章
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38263 Accepted: 11891 ...
- poj-3278 catch that cow(搜索题)
题目描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...
- POJ - 3278 Catch That Cow 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
- Catch The Caw——(广度优先搜索的应用,队列)
抓住那头牛(POJ3278)农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0<=N<=100000),牛位于点K(0<=K<=100000).农夫有 ...
- Catch That Cow 分类: POJ 2015-06-29 19:06 10人阅读 评论(0) 收藏
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 58072 Accepted: 18061 ...
- Poj 3287 Catch That Cow(BFS)
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
随机推荐
- F28379D烧写双核程序(在线&离线)
烧写双核程序前需知在分别对F28379D的CPU1和CPU2两个核进行烧写程序时,需要在CCS中建立两个工程,独立编写两个核的程序.如controlSUITE中提供的双核程序例程: 1. 在线1.1 ...
- Android 系统启动过程详解
android 使用 linux 内核,一般运行在 ARM 体系架构上,android 设备启动的过程,应用层之下基本等同于linux, 从应用层第一个程序init开始有所区别,下面开始介绍. ste ...
- python每日一类(1):pathlib
每天学习一个python的类(大多数都是第三方的),聚沙成金. -------------------------------------------------------------------- ...
- Python的程序结构[8] -> 装饰器/Decorator -> 装饰器浅析
装饰器 / Decorator 目录 关于闭包 装饰器的本质 语法糖 装饰器传入参数 1 关于闭包 / About Closure 装饰器其本质是一个闭包函数,为此首先理解闭包的含义. 闭包(Clos ...
- 腾讯消消乐 (状态压缩DP)
腾讯消消乐 题意 给出长度为 n 的序列,每次可以选择删除序列的一个连续区间,要求这一段区间内所有数最大公约数不小于 k ,删除后剩下的序列仍然构成连续序列. 定义 f(i) 为进行 i 次操作将整个 ...
- java有自动垃圾回收机制
当垃圾收集器判断已经没有任何引用指向对象的时候,会调用对象的finalize方法来释放对象占据的内存空间~ java中垃圾回收以前听老师讲好像是内存满了他才去做一次整体垃圾回收,在回收垃圾的同时会调用 ...
- [HDU4336]Card Collector(min-max容斥,最值反演)
Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- [CP1804]组合数问题2
题目大意: 给定两个数$n(n\le10^6)$和$k(k\le10^5)$,找到$k$个不同的满足$0\le b\le a\le n$的组合数$\binom a b$,求这$k$个组合数的最大值. ...
- 用swift开发自己的MacOS锁屏软件(3)
前两篇中实现了MacOS端的锁屏软件,现在需要再实现一个移动端的app用来实现和mac的通信,以后的文章可能就会两个项目来回穿插了. 写完MacOS的软件又回来接着写iOS真的是享受,看着堆积如山的各 ...
- sqlite db-journal文件产生原因及说明
今天在Android中将sqlite的数据库文件生成在SD卡上的过程中,发现生成的.db文件的旁边 生成了一个大小为0的与数据库文件同名的.db-journal文件,不明白此文件的用途,于是 goog ...