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 ...
随机推荐
- Windows消息类型
WM_ Window Message 窗口消息,一般用在SendMessage,PostMessage这样的消息函数中 SM_ Static Message 静态标签消息 SS_ Static Sty ...
- PSR-2 编码风格规范
本篇规范是 PSR-1 基本代码规范的继承与扩展. 本规范希望通过制定一系列规范化PHP代码的规则,以减少在浏览不同作者的代码时,因代码风格的不同而造成不便. 当多名程序员在多个项目中合作时,就需要一 ...
- Codeforces 914D Bash and a Tough Math Puzzle (ZKW线段树)
题目链接 Round #458 (Div. 1 + Div. 2, combined) Problem D 题意 给定一个序列,两种询问:单点修改,询问某个区间能否通过改变最多一个数使得该区间的 ...
- Python与数据库[1] -> 数据库接口/DB-API[0] -> 通用标准
数据库接口 / DB-API 在Python中,数据库是通过适配器(Adaptor)来连接访问数据库的,适配器通常与数据库客户端接口(通常为C语言编写)想连接,而不同的适配器都会尽量满足相同的DB-A ...
- Python与数据结构[1] -> 栈/Stack[1] -> 中缀表达式与后缀表达式的转换和计算
中缀表达式与后缀表达式的转换和计算 目录 中缀表达式转换为后缀表达式 后缀表达式的计算 1 中缀表达式转换为后缀表达式 中缀表达式转换为后缀表达式的实现方式为: 依次获取中缀表达式的元素, 若元素为操 ...
- 如何理解java反射?
一.反射基本概念 反射之中包含了一个"反"的概念,所以要想解释反射就必须先从"正"开始解释,一般而言,当用户使用一个类的时候,应该先知道这个类,而后通过这个类产 ...
- Codeforces 1037F. Maximum Reduction
总感觉我这种做法会T,一直没写,看了其他人的题解也是这样,,,就果断写了,,可能数据不太深,或者玄学复杂度 题意即求xk-1长度的所有区间的最大值的和,对每一个i(数组下边),他对答案的贡献数量就是在 ...
- the-swift-programming-language 学习笔记
常量和变量 常量是定义是不可以修改的,在类中定义的常量,可以在构造函数中赋值.let修饰变量是可以修改的.var修饰字符串中字符的遍历for code in string {}for codeunit ...
- First Missing Positive -- LeetCode
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- [TJOI2009] 战争游戏
题目背景 小R正在玩一个战争游戏.游戏地图是一个M行N列的矩阵,每个格子可能是障碍物,也可能是空地,在游戏开始时有若干支敌军分散在不同的空地格子中.每支敌军都可以从当前所在的格子移动到四个相邻的格子之 ...