poj 3278:Catch That Cow(简单一维广搜)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 45648 | Accepted: 14310 |
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
Source
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std; bool isw[]; struct Node{
int x;
int s;
}; bool judge(int x)
{
if(x< || x>)
return true;
if(isw[x])
return true;
return false;
} int bfs(int sta,int end)
{
queue <Node> q;
Node cur,next;
cur.x = sta;
cur.s = ;
isw[cur.x] = true;
q.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
if(cur.x==end)
return cur.s;
//前后一个个走
int nx;
nx = cur.x+;
if(!judge(nx)){
next.x = nx;
next.s = cur.s + ;
isw[next.x] = true;
q.push(next);
}
nx = cur.x-;
if(!judge(nx)){
next.x = nx;
next.s = cur.s + ;
isw[next.x] = true;
q.push(next);
}
//向前跳
nx = cur.x*;
if(!judge(nx)){
next.x = nx;
next.s = cur.s + ;
isw[next.x] = true;
q.push(next);
}
}
return ;
} int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF){
memset(isw,,sizeof(isw));
int step = bfs(n,k);
printf("%d\n",step);
}
return ;
}
Freecode : www.cnblogs.com/yym2013
poj 3278:Catch That Cow(简单一维广搜)的更多相关文章
- POJ - 3278 Catch That Cow 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
- POJ 3278 Catch That Cow(简单BFS)
题目链接:http://poj.org/problem?id=3278 题目大意:给你两个数字n,k.可以对n执行操作(n+1,n-1,n*2),问最少需要几次操作使n变成k. 解题思路:bfs,每次 ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- poj 3278 Catch That Cow (广搜,简单)
题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
随机推荐
- 项目:DoubleFaceCamera
代码在github: https://github.com/Viyu/DoubleFacedCamera 这个app的想法是:用手机的前置和后置摄像头同时拍摄画面合成输出一张图,把拍摄者和被拍摄者的画 ...
- 简单的哈希表实现 C语言
简单的哈希表实现 简单的哈希表实现 原理 哈希表和节点数据结构的定义 初始化和释放哈希表 哈希散列算法 辅助函数strDup 哈希表的插入和修改 哈希表中查找 哈希表元素的移除 哈希表打印 测试一下 ...
- poj3984
定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...
- ndk学习5: ndk中使用c++
默认情况下ndk不支持标准C++库,异常, rtti等 在ndk文档有关于C++ support的详细介绍 一. 使用C++标准库 介绍: 默认是使用最小额度的C++运行时库, 在Applic ...
- 《转载》化繁为简 如何向老婆解释MapReduce?
本文转载自http://server.zol.com.cn/329/3295529.html 昨天,我在Xebia印度办公室发表了一个关于MapReduce的演说.演说进行得很顺利,听众们都能够理解M ...
- 小波变换C++实现(一)----单层小波变换
文章转自: http://www.cnblogs.com/IDoIUnderstand/archive/2013/03/30/3280724.html [小波变换]STL版 一维离散小波变换(DWT) ...
- Appium+Robotframework实现Android应用的自动化测试-1:Appium在Windows中的安装
让我们开始在Windows中开始安装Appium吧,Appium在OS X中的具体安装后面的文章会介绍. 另外,官网上说先要装Node.js,还要装Apache Ant和Apache Maven,Gi ...
- java切换VPN让你像幽灵一样出现在全国各地
在很多情况下,有些网络应用的需求会要求模拟人在不同地区访问网站和应用.因而切换IP也就应运而生了,然而IP作为一种稀缺资源不是随便可以获得的.因而会想到应用程序切换VPN来达到全国不同地区访问网络.因 ...
- Divide and Conquer:Monthly Expense(POJ 3273)
Monthly Expense 题目大意:不废话,最小化最大值 还是直接套模板,不过这次要注意,是最小化最大值,而不是最大化最小值,判断的时候要注意 联动3258 #include <iostr ...
- 数据结构-链表实现删除全部特定元素x
链表节点类定义: template <class T> class SingleList; template <class T> class Node { private: T ...