[poj] Catch That Cow--bfs
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
三个搜索方向 +1, -1, *2 用bfs搜索 数组要开到最大值的2倍 注意剪枝和边界问题
#include <iostream>
#include <stdio.h>
#include <queue>
#include <cstring>
using namespace std; const int Max = ;
int s, e;
struct node
{
int x, step;
}now, Next;
bool v[];
queue<node>q; int bfs()
{
now.x = s;
now.step = ;
v[s] = ;
q.push(now);
while (!q.empty()) {
now = q.front();
q.pop();
if (now.x == e)
return now.step; Next.x = now.x*;
Next.step = now.step+;
if (Next.x >= && Next.x <= Max && !v[Next.x]){
q.push(Next);
v[Next.x] = ;
} Next.x = now.x-;
Next.step = now.step+;
if (Next.x >= && Next.x <= Max && !v[Next.x]){
q.push(Next); //若先检查v[Next.x] 会出现数组越界v[-1]的情况
v[Next.x] = ;
} Next.x = now.x+;
Next.step = now.step+;
if (Next.x >= && Next.x <= Max && !v[Next.x]){
q.push(Next);
v[Next.x] = ;
} }
return ;
} int main()
{
//freopen("1.txt", "r", stdin);
cin >> s >> e;
memset(v, , sizeof(v));
cout << bfs(); return ;
}
[poj] Catch That Cow--bfs的更多相关文章
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- POJ - 3278 Catch That Cow BFS求线性双向最短路径
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- POJ3278——Catch That Cow(BFS)
Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...
- catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38263 Accepted: 11891 ...
- poj 3278 Catch That Cow bfs
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
随机推荐
- 【Android】Android中AlertDialog对话框的使用实例
package com.ceac.deng; import android.R.string; import android.support.v7.app.ActionBarActivity; imp ...
- main方法的参数
敲例子的时候无意中把主方法的参数给落下了,当时没有发现,保存之后就去编译,运行了,通常情况下编译没有错误那胜利就在掌握之中了,没想到这次我竟然在"不一般"的行列中,编译无误,运行出 ...
- web项目中从不同的路径读取文件
项目中的配置文件可以放在classpath下,webapp下获取其他任何一个指定的绝对地址,读取这些文件就从这三个地方去找.主要代码如下: private List<String> get ...
- HDU - 3068 最长回文 【Manacher】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3068 思路 n^3 的做法 对于每个字符 同时 往左往右搜 但是要分奇偶 就是 n^3 n^2 的做法 ...
- BZOJ 4868-4873 题解
BZOJ4868 每个结束位置的最优值很显然具有单调性,三分,再讨论一下就好了. #include<bits/stdc++.h> using namespace std; #define ...
- 算法(Algorithms)第4版 练习 1.3.14
方法实现: //1.3.14 package com.qiusongde; import java.util.Iterator; import java.util.NoSuchElementExcep ...
- 草原psd素材
草原PSD素材,草原,风景,蓝天白云,飞鸟,阳光,绿色,草地. http://www.huiyi8.com/caoyuan/psd/
- 用gdisk调整gpt/ext4分区大小
主机: CentOS release 6.4 (Final) 目的:从/home分区分出100G来创建新分区/vm 参考: http://ryanclouser.com/?p=66 http://fa ...
- spark源码笔记
1.国际化 如添加朋友Friends是英文,可以找着相关的类,并在国际化配置文件中添加key 在项目中全局搜索“Friends”,将得到的结果集全部展开,找到这两个文件: 在国际化配置文件spark_ ...
- SpringMVC拦截器的配置与使用详解
一.SpringMVC拦截器简介 Spring MVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理.在springmvc中,定义拦截 ...