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 ...
随机推荐
- 用 gdb 调试 GCC 程序【转】
用 GDB 调试程序 原著:Rick McMullin 用 gdb 调试 GCC 程序 转自:http://blog.csdn.net/bonnshore/article/details/795542 ...
- python的tips:字符和字符串的问题
今天,自己建立了一个redis,python去访问的时候, 设置可以key以后,再读取key,返回的是字符, 和字符串比较,需要做一个转换, 信息如下: import redisr=redis.Red ...
- JavaScript 性能优化的小知识总结
前言 一直在学习 javascript,也有看过<犀利开发 Jquery 内核详解与实践>,对这本书的评价只有两个字犀利,可能是对 javascript 理解的还不够透彻异或是自己太笨,更 ...
- (5)C#运算符
运算符 参照javase (7)java基础知识-原码.反码.补码.运算符
- 二分LIS模板
假设存在一个序列d[1..9] = 2 1 5 3 6 4 8 9 7,可以看出来它的LIS长度为5. 下面一步一步试着找出它. 我们定义一个序列B,然后令 i = 1 to 9 逐个考察这个序列. ...
- 通过命令行上传代码到GitHub
自工作以来,本人第一次使用GitHub.下面是将本地的项目上传到GitHub的过程.上传代码的前提是:1.已注册GitHub账号:2.本地已安装Git. 第一步:远程Git仓库 进入本地的项目的根目录 ...
- StringBuffer 清空
几种方法: 方法1: 1 2 3 4 StringBuffer my_StringBuffer = new StringBuffer(); my_StringBuffer.append('hellow ...
- [TJOI2009] 战争游戏
题目背景 小R正在玩一个战争游戏.游戏地图是一个M行N列的矩阵,每个格子可能是障碍物,也可能是空地,在游戏开始时有若干支敌军分散在不同的空地格子中.每支敌军都可以从当前所在的格子移动到四个相邻的格子之 ...
- unity3d 场景配置文件生成代码
using UnityEngine; using UnityEditor; using System.IO; using System; using System.Text; using System ...
- Java StringBuffer与StringBuider
String 的值是不可变的,每次对String的操作都会生成新的String对象,不仅效率低,而且耗费大量内存空间. StringBuffer类和String类一样,也用来表示字符串,但是Strin ...