POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow
Time Limit: 2000MS |
Memory Limit: 65536K |
|
---|---|---|
Total Submissions: 88732 |
Accepted: 27795 |
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
Line 1: Two space-separated integers: N and K
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample Input
5 17
Sample Output
4
Hint
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
Source
题目链接:http://poj.org/problem?id=3278
题意:有一个农民和一头牛,他们在一个数轴上,牛在k位置保持不动,农户开始时在n位置。设农户当前在M位置,每次移动时有三种选择:1.移动到M-1;2.移动到M+1位置;3.移动到M*2的位置。问最少移动多少次可以移动到牛所在的位置。所以可以用广搜来搜索这三个状态,直到搜索到牛所在的位置。
分析:BFS的板子题,用队列做,第一次学队列,感觉自己好弱啊!现在才学,算法真的好难学,学了又怕不会用,现在不敢学太快了!
下面每一步代码我都给出了详细解释,一看就懂!
1 #include<iostream>
2 #include <algorithm>
3 #include <stdio.h>
4 #include <string.h>
5 #include<queue>
6 #define MAX 100001
7 using namespace std;
8 queue<int> q;//使用前需定义一个queue变量,且定义时已经初始化
9 bool visit[MAX];//访问空间
10 int step[MAX]; //记录步数的数组不能少
11 bool bound(int num)//定义边界函数
12 {
13 if(num<0||num>100000)
14 return true;
15 return false;
16 }
17 int BFS(int st,int end)
18 {
19 queue<int> q;//使用前需定义一个queue变量,且定义时已经初始化
20 int t,temp;
21 q.push(st);//进队列
22 visit[st]=true;
23 while(!q.empty())//重复使用时,用这个初始化
24 {
25 t=q.front();//得到队首的值
26 q.pop();//出队列,弹出队列的第一个元素,并不会返回元素的值
27 for(int i=0;i<3;++i) //三个方向搜索
28 {
29 if(i==0)
30 temp=t+1;
31 else if(i==1)
32 temp=t-1;
33 else
34 temp=t*2;
35 if(bound(temp)) //越界
36 continue;
37 if(!visit[temp])//访问空间未被标记
38 {
39 step[temp]=step[t]+1;
40 if(temp==end)
41 return step[temp];
42 visit[temp]=true;//标记此点
43 q.push(temp);//将temp元素接到队列的末端;
44 }
45 }
46 }
47 }
48 int main()
49 {
50 int st,end;
51 while(scanf("%d%d",&st,&end)!=EOF)
52 {
53 memset(visit,false,sizeof(visit));//visit数组进行初始化操作
54 if(st>=end)
55 cout<<st-end<<endl;
56 else
57 cout<<BFS(st,end)<<endl;
58 }
59 return 0;
60 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 一道Postgresql递归树题
- 突击并发编程JUC系列-JDK1.8 扩展类型 LongAdder
- 利用tensorflow训练简单的生成对抗网络GAN
- 《Java从入门到失业》第三章:基础语法及基本程序结构(3.7):运算符(基本算数运算符、原码、反码、补码)
- 《Java从入门到失业》第三章:基础语法及基本程序结构(3.6):基本数据类型及字符集编码(字符编码和char型)
- 《Java从入门到失业》第三章:基础语法及基本程序结构(3.6):基本数据类型及字符集编码(整型、浮点型、布尔型)
- 多图详解Spring框架的设计理念与设计模式
- 《Java从入门到失业》第三章:基础语法及基本程序结构(3.2-3.5):标识符、关键字、注释、变量及常量
- 《Java从入门到失业》第三章:基础语法及基本程序结构(3.1):一个简单的例子
- 详解Java解析XML的四种方法
- 《Java从入门到失业》第四章:类和对象(4.6):类路径
- 《Java从入门到失业》第四章:类和对象(4.5):包
- 《Java从入门到失业》第四章:类和对象(4.4):方法参数及传递
- 《Java从入门到失业》第四章:类和对象(4.3):一个完整的例子带你深入类和对象
- 《Java从入门到失业》第四章:类和对象(4.2):String类