catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 38263 | Accepted: 11891 |
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>
#include<stdlib.h>
#include<string.h>
struct vode
{
int time,weizhi;
};
struct vode que[];
int main()
{
int l,r,weizhi;
int n,k;
scanf("%d%d",&n,&k);
l=,r=;
weizhi=n;
que[r].weizhi=weizhi;
que[r].time=;
r++;
int flag[]={};
flag[weizhi]=;
while(que[l].weizhi!=k)
{
weizhi=que[l].weizhi;
if(weizhi->=&&weizhi-<=&&flag[weizhi-]==)
{
que[r].weizhi=que[l].weizhi-;
que[r].time=que[l].time+;
flag[weizhi-]=;
r++;
}
if(weizhi+>=&&weizhi+<=&&flag[weizhi+]==)
{
que[r].weizhi=que[l].weizhi+;
que[r].time=que[l].time+;
flag[weizhi+]=;
r++;
}
if(weizhi*>=&&weizhi*<=&&flag[weizhi*]==)
{
que[r].weizhi=que[l].weizhi*;
que[r].time=que[l].time+;
flag[weizhi*]=;
r++;
}
l++;
}
printf("%d\n",que[l].time);
return ;
}
catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)的更多相关文章
- 数据结构学习笔记05图 (邻接矩阵 邻接表-->BFS DFS、最短路径)
数据结构之图 图(Graph) 包含 一组顶点:通常用V (Vertex) 表示顶点集合 一组边:通常用E (Edge) 表示边的集合 边是顶点对:(v, w) ∈E ,其中v, w ∈ V 有向边& ...
- poj-3278 catch that cow(搜索题)
题目描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...
- Catch That Cow(广度优先搜索_bfs)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 48036 Accepted: 150 ...
- 【数据结构】图的基本操作——图的构造(邻接矩阵,邻接表),遍历(DFS,BFS)
邻接矩阵实现如下: /* 主题:用邻接矩阵实现 DFS(递归) 与 BFS(非递归) 作者:Laugh 语言:C++ ***************************************** ...
- POJ - 3278 Catch That Cow 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
- 图 邻接表 邻接矩阵 BFS生成树 DFS生成树
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- 数据结构(11) -- 邻接表存储图的DFS和BFS
/////////////////////////////////////////////////////////////// //图的邻接表表示法以及DFS和BFS //////////////// ...
- 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)
一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...
随机推荐
- 编码(Code)
很遗憾,直接搜索Code或者编码是很难得找到这本书的,我也是无意中才接触到本书. 第一次读本书,对各方面的知识都不算很懂,觉得很多地方都写的太多浅显,好像本该就是这样子,一个编码系统说的那么麻烦干嘛, ...
- 11.5---含有空字符串的字符串查找(CC150)
注意,1,"" 和 " ".是不同的,空字符串指的是"": 2,注意String的compareTo.小于是指<0.并不是==-1: ...
- 解决vi/vim中粘贴会在行首多很多缩进和空格的问题
解决vi/vim中粘贴会在行首多很多缩进和空格的问题 secureCRT会将你原来的文本原封不动的按照字符串的样式发送给服务器.所以当你的服务器上的vim设置为autoindent的话,在i模式下,那 ...
- yum服务器搭建(深入理解yum工作原理)
作者:firefoxbug 时间:July 27, 2014 分类:Linux 前言 在前面一篇rpm包制作描述了rpm的打包过程,这篇文章主要讲述yum的工作原理. yum 运行原理 yum的工作需 ...
- SQL Server日期和时间的格式
在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...
- C#零碎知识汇总
1.1取时间差 时刻:DateTime 时差:TimeSpan 代码: DateTime time1 = DateTime.Now; textBox1.Text = time1.ToStri ...
- struts2 如何实现mvc 的?
- 常见概率组合题目总结quickstart
[本文链接] http://www.cnblogs.com/hellogiser/p/interview-questions-quickstart-for-combination-permutatio ...
- 用Java计算某个日期100天后的日期
用Java计算日期时间,首先考虑到时间类,Java中提供的和时间日期相关的类里,有一个Calendar类,可以获取某个时间日期. 计算某个日期100天后的日期,思路如下: 1.先设定时间,例如2017 ...
- VMware ESXi客户端连接控制台时提示"VMRC控制台连接已断开...正在尝试重新连接"的解决方法
通过vSphere Client连接到安装VMware ESXi虚拟环境的主机时,当启动操作系统,选中控制台时控制台上方提示一行"VMRC控制台的连接已断开...正在尝试重新连接" ...