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
Output
Sample Input
5 17
Sample Output
4
Hint
Source
#include<iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include<queue>
#define MAX 100001
using namespace std;
queue<int> q;//使用前需定义一个queue变量,且定义时已经初始化
bool visit[MAX];//访问空间
int step[MAX]; //记录步数的数组不能少
bool bound(int num)//定义边界函数
{
if(num<||num>)
return true;
return false;
}
int BFS(int st,int end)
{
queue<int> q;//使用前需定义一个queue变量,且定义时已经初始化
int t,temp;
q.push(st);//进队列
visit[st]=true;
while(!q.empty())//重复使用时,用这个初始化
{
t=q.front();//得到队首的值
q.pop();//出队列,弹出队列的第一个元素,并不会返回元素的值
for(int i=;i<;++i) //三个方向搜索
{
if(i==)
temp=t+;
else if(i==)
temp=t-;
else
temp=t*;
if(bound(temp)) //越界
continue;
if(!visit[temp])//访问空间未被标记
{
step[temp]=step[t]+;
if(temp==end)
return step[temp];
visit[temp]=true;//标记此点
q.push(temp);//将temp元素接到队列的末端;
}
}
}
}
int main()
{
int st,end;
while(scanf("%d%d",&st,&end)!=EOF)
{
memset(visit,false,sizeof(visit));//visit数组进行初始化操作
if(st>=end)
cout<<st-end<<endl;
else
cout<<BFS(st,end)<<endl;
}
return ;
}
POJ 3278 Catch That Cow(BFS,板子题)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- POJ 3278 Catch That Cow bfs 难度:1
http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #inclu ...
- POJ - 3278 Catch That Cow bfs 线性
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
随机推荐
- 【java】用HashMap计数,用TreeSet排序
package com.tn.hashMap; import java.util.HashMap; import java.util.TreeSet; public class HashMapDemo ...
- (精选)Xcode极速代码,征服Xcode,xcode插件
插件 1 http://blog.csdn.net/qq_30513483/article/details/52349997 插件2 http://www.code4app.com/forum.ph ...
- iOS MJRefresh上拉加载更多
1.导入MJRefresh包 2.在类中引入:#import "MJRefresh.h" 3.添加footerView 添加加载更多的UI样式: MJRefreshAutoNorm ...
- ecshop中的$user对象
ecshop的程序中,有个对象:$user,它是用来处理用户信息的.比如登录.注册,还有就是用来和第三方管理通讯和共享资源的.在user.php中,有一条$user->login($userna ...
- 西门子flexable创建画面
一.wincc flexable 创建画面包括以下四点 二.具体操作 1.组态画面模板 1)使用该模板的画面包括该模板的所有组件,一个模板也是一个画面 2)给模板上添加一个文本域如下图,则画面1也会显 ...
- ArcGIS API for JavaScript 4.2学习笔记[11] 官方第五章Popups(弹窗)概览与解释
直接跳过第三第四章了,第三章Layer和第四章可视化,怎么说呢,Layer是组织数据的,是Map的属性之一.可视化属于符号化编程,暂时不看. 第五章是对数据.结果的显示,类似于alert()..NET ...
- openstack操作之一 命令行
在openstack环境中提供了多种操作虚拟机的方法,有最简单直接的dashborad界面,有不直观但高效的命令行,还有进阶版的postman调用openstack restfulapi和命令行中使用 ...
- (一)DOM 常用操作 —— “查找”节点
在 DOM 树中,如果想要操作一个节点,那么首先要"查找"到这个节点.查找节点的方法由 Document 接口定义,而该接口由 JavaScript 中的 document 对象实 ...
- node基础篇二:模块、路由、全局变量课堂(持续)
今天继续更新node基础篇,今天主要内容是模块.路由和全局变量. 模块这个概念,在很多语言中都有,现在模块开发已经成为了一种潮流,它能够帮助我们节省很多的时间,当然咱们的node自然也不能缺少,看下例 ...
- Scrum And Teamwork
Scrum Learning 概念 Scrum是迭代式增量软件开发过程,通常用于敏捷软件开发.Scrum包括了一系列实践和预定义角色的过程骨架.Scrum中的主要角色包括同项目经理类似的Scrum主管 ...