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) ...
随机推荐
- Raspberry Pi 学习笔记之一
记录几个搭建服务器教程: [教程] 将树莓派变成网络打印机服务器(更新PC和手机端设置)!http://www.eeboard.com/bbs/thread-8818-1-1.html[心得] [分享 ...
- hdu1536&&hdu3023 SG函数模板及其运用
S-Nim Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status ...
- 一个Try多个Catch需要注意的事项
一个程序包含一个try块和两个catch块,两个catch子句都有能力捕捉一个try块发出的异常,若两个catch子句次序不同时程序结果会发生变化吗? 一个try块后有两个catch块,这很正常,因为 ...
- MQTT V3.1----publish解读
客户端/服务器的发布消息行为,与PUBLISH相关的消息类型: PUBLISH 客户端发布消息经由服务器分发到所有对应的订阅者那里.一个订阅者可以订阅若干个主题(Topic name),但一个PUBL ...
- C#之Windows消息处理
public enum WindowsMessage:int { /// <summary> /// /// </summary> WM_NULL = 0x0000, /// ...
- Django~Settings.py
配置 数据库 默认sqlite, 支持Mysql,postgresql,oracle 更改时区 查看表结构 .schema (SQLite), display the tables Django cr ...
- 从json传递数据显示表格实例
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate> { UITableView* table; ...
- Cuckoo for Hashing_双哈希表
问题 B: Cuckoo for Hashing 时间限制: 1 Sec 内存限制: 64 MB提交: 24 解决: 12[提交][状态][讨论版] 题目描述 An integer hash ta ...
- ASM:《X86汇编语言-从实模式到保护模式》1-4章:处理器,内存和硬盘基础
其实很久之前就学完了实模式了,但是一直没有总结,感觉现在直接在书上做笔记的弊端就是有些知识点不能很很深刻地记下来(毕竟手写最明显的优点就是能深刻地记住知识,但是就是用太多的时间罢了).一下内容都是一些 ...
- windows下ftp命令大全
FTP Server: home4u.at.china.com User: yepanghuang Password: abc123 打开Windows的开始菜单,执行“运行”命令,在对话框中输入ft ...