(广搜)Catch That Cow -- poj -- 3278
链接:
http://poj.org/problem?id=3278
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 62113 | Accepted: 19441 |
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
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue> using namespace std; #define N 110000 struct node
{
int x, step;
}; int s, e;
bool vis[N]; int BFS(int s)
{
node p, q;
p.x = s, p.step = ; memset(vis, false, sizeof(vis));
vis[s] = true;
queue<node>Q;
Q.push(p); while(Q.size())
{
p = Q.front(), Q.pop(); if(p.x == e) return p.step; for(int i=; i<; i++)
{
if(i==)
q.x = p.x + ;
else if(i==)
q.x = p.x - ;
else if(i==)
q.x = p.x * ; q.step = p.step + ;
if(q.x>= && q.x<N && !vis[q.x])
{
Q.push(q);
vis[q.x] = true;
}
}
} return -;
} int main()
{
while(scanf("%d%d", &s, &e)!=EOF)
{
int ans = BFS(s); printf("%d\n", ans);
}
return ;
}
(广搜)Catch That Cow -- poj -- 3278的更多相关文章
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
- Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
- Catch That Cow POJ - 3278 bfs map超时,短路判断顺序。
题意:可以把n边为n+1,n-1,n*2问从n到k的最少变化次数. 坑:标题写了.有点不会写bfs了... ac代码 #define _CRT_SECURE_NO_WARNINGS #include& ...
- C - Catch That Cow POJ - 3278
//标准bfs #include <iostream> #include <cstdio> #include <algorithm> #include <cm ...
- kuangbin专题 专题一 简单搜索 Catch That Cow POJ - 3278
题目链接:https://vjudge.net/problem/POJ-3278 题意:人可以左移动一格,右移动一格,或者移动到当前位置两倍下标的格子 思路:把题意的三种情况跑bfs,第一个到达目的地 ...
- (广搜)Dungeon Master -- poj -- 2251
链接: http://poj.org/problem?id=2251 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2137 ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- 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 ...
随机推荐
- MySQL This function has none of DETERMINISTIC, NO SQL...错误1418 的原因分析及解决方法
MySQL开启bin-log后,调用存储过程或者函数以及触发器时,会出现错误号为1418的错误: ERROR 1418 (HY000): This function has none of DETER ...
- SQL Server中动态列转行
http://www.cnblogs.com/gaizai/p/3753296.html 一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现 ...
- cocos2dx继承结构图
包含关系 CCDirector->CCScene->CCLayer->CCSprite->CCAction 继承关系 CCObject---CCAction(动作,控制图层运动 ...
- php删除制定文件及文件夹
php遍历一个文件夹内的所有文件和文件夹,并删除所有文件夹和子文件夹下的所有文件的代码,通过递归方式实现达到清空一个目录的效果,代码简单实用. 用到的函数: scandir($path) 遍历一个文件 ...
- yyblog2.0 数据库开发规范
一.基础规范 (1)必须使用InnoDB存储引擎 解读:支持事务.行级锁.并发性能更好.CPU及内存缓存页优化使得资源利用率更高 (2)表字符集默认使用utf8,必要时候使用utf8mb4 解读:1. ...
- linux编程vim设置
linux环境下c网络编程vim编辑工具设置,包括自动缩进,tab键对齐等.
- asp.net,C#操作数据库DataTable关于空null的判断
double d=0;if(!Convert.IsDBNull(DataTable.Rows[i][m])){ string str=DataTable.Rows[i][m].ToString( ...
- 在VS2005编程中,有的时候DataGridView数据源有几个表的联合查询,而系统又有限制为一个表,怎么办?
在VS2005编程中,有的时候DataGridView数据源有几个表的联合查询,而系统又有限制为一个表,怎么办? 解决方法:在SqlServer的企业管理器里增加一个视图吧!!!!!!!!(从来没用过 ...
- mac os 平台下载并编译android2.3.3源码
现在在做有关android平台下的项目,最初对android环境各种不熟悉,搞了几个月终于有点眉目了,由于需要用到android本身提供的一些类似gps,tts等服务,单纯的看android提供的ja ...
- How to run eclipse in clean mode? and what happens if we do so?
What it does: if set to "true", any cached data used by the OSGi framework and eclipse run ...