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 ...
随机推荐
- 【二十一】基于mysqli的表格数据练习
mysqlitest.php <?php //调用数据库的函数 function connetionsql(){ $conn=mysqli_connect("127.0.0.1&quo ...
- ES6之Set方法与Map方法
ES6提供了新的数据结构--Set与Map,Set本身是一个构造函数且成员的值是唯一的,没有重复的值!!!Set()是一个存储已排序的无重复元素的数据而Map()是一对数据Map()使用关键值Key来 ...
- JavaWeb框架_Struts2_(八)----->Struts2的国际化
这一篇博文拖了蛮久了,现在先把它完成,结束struts2这个版块,当然这只是最基础的部分,做项目还需要更深的理解.下一个web后端的版块准备做Spring框架的学习-嗯,加油! 1. Struts2的 ...
- Java JTS & 空间数据模型
空间数据模型 判断两个几何图形是否存在指定的空间关系.包括: 相等(equals).分离(disjoint).相交(intersect).相接(touches).交叉(crosses).包含于(wit ...
- 如何录屏制作gif图片
LICEcap简介: LICEcap是一款屏幕录制制作gif图片的工具,支持导出 GIF 动画图片格式,轻量级.使用简单,录制过程中可以随意改变录屏范围. LICEcap 直观易用,功能灵活,支持 W ...
- sublime学习笔记
学习课程地址:快乐的sublime编辑器_sublime编辑器使用 另可参考笔记地址:http://c.haoduoshipin.com/happysublime/ PS:博主的一些文章地址:http ...
- RabbitMQ之消费者Demo(队列参数详细说明)
package com.jiefupay; import java.io.IOException; import java.util.HashMap; import java.util.Map; 8 ...
- linux下制作镜像文件
第一:制作iso镜像的方法: 把/dev/cdrom目录制作为镜像,名字为/root/rh1.iso 方法1:dd if=/dev/cdrom of=/root/rh1.iso 方法2:#cat /d ...
- c#全宇宙最牛的编程软件
c#走的道路!PC,PD,电脑一体,一个账户就可以三合一,可以跨平台的编程,在未来的道路如果微软能一直走下去,那么c#将成为宇宙最牛B的编程软件.
- unity demo之坦克攻击
先展示一下成果吧,节后第一天上班简直困爆了,所以一定要动下脑子搞点事情. 分析: 1.涉及到的游戏对象有:坦克,摄像机,场景素材(包含灯光),子弹 2.坦克具有的功能:移动,旋转,发射子弹,记录生命值 ...