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扩展类批量执行多条sql语句
批量处理多个sql语句 <?php //批量执行多个dml语句 // $mysqli=new mysqli("localhost","root",&quo ...
- CSS文字不换行,溢出省略
white-space:nowrap; overflow:hidden; text-overflow:ellipsis;
- mkpasswd 随机密码生成
root@op-admin:~# mkpasswd -l -n usage: mkpasswd [args] [user] where arguments are: -l # (length of p ...
- 为什么epoll会那么高效
参考(原文简直超赞):https://zhidao.baidu.com/question/687563051895364284.html下面是我结合原文写的,为了便于自己理解:关于阻塞和非阻塞的理解可 ...
- 使用sed,grep 批量修改文件内容
使用sed命令可以进行字符串的批量替换操作,以节省大量的时间及人力: 使用的格式如下: sed -i "s/oldstring/newstring/g" `grep oldstri ...
- ASP.NET Core读取AppSettings
http://www.tuicool.com/articles/rQruMzV 今天在把之前一个ASP.NET MVC5的Demo项目重写成ASP.NET Core,发现原先我们一直用的Configu ...
- Perl 中 `cmd` 和system"cmd"的区别
在perl中,调用系统命令有两种形势,`cmd` 和system"cmd",他们主要的区别是`cmd`会获取返回结果,而system"cmd"会直接将结果输出到 ...
- Android真机安装sqlite3的方法
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- Heroku 如何上重置 PostgreSQL 数据库
如果你要在 Heroku 上重置 PostgreSQL 数据库,可以使用以下命令 : $ heroku pg:reset DATABASE $ heroku run php artisan mig ...
- JS中call,apply,bind方法的总结
why?call,apply,bind干什么的?为什么要学这个? 一般用来指定this的环境,在没有学之前,通常会有这些问题. var a = { user: "小马扎", fn: ...