POJ 3278: Catch That Cow
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 44613 | Accepted: 13946 |
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
题意:有一个农民和一头牛,他们在一个数轴上,牛在k位置保持不动,农户開始时在n位置。设农户当前在M位置,每次移动时有三种选择:1.移动到M-1。2.移动到M+1位置;3.移动到M*2的位置。问最少移动多少次能够移动到牛所在的位置。所以能够用BFS来搜索这三个状态,直到搜索到牛所在的位置。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; const int N = 200100;
int n, k;
struct node
{
int x, step;
};
queue<node> q;
int vist[N]; void bfs()
{
int cow, ans;
while(!q.empty())
{
node tmp = q.front();
q.pop();
cow = tmp.x;
ans = tmp.step;
if(cow == k)
{
printf("%d\n",ans);
return ;
}
if(cow >= 1 && !vist[cow - 1]) //要保证减1后有意义,所以要cow >= 1 减一的情况
{
node temp;
vist[cow - 1] = 1;
temp.x = cow - 1;
temp.step = ans + 1;
q.push(temp);
}
if(cow <= k && !vist[cow + 1]) //加1的情况
{
node temp;
vist[cow + 1] = 1;
temp.x = cow + 1;
temp.step = ans + 1;
q.push(temp);
}
if(cow <= k && !vist[cow * 2]) //乘二的情况
{
node temp;
vist[cow * 2] = 1;
temp.x = 2 * cow;
temp.step = ans + 1;
q.push(temp);
}
}
} int main()
{
while(~scanf("%d%d",&n,&k))
{
while(!q.empty()) q.pop();
memset(vist,0,sizeof(vist));
vist[n] = 1;
node t;
t.x = n, t.step = 0;
q.push(t);
bfs();
}
return 0;
}
POJ 3278: Catch That Cow的更多相关文章
- POJ 3278:Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submi ...
- POJ 3278:The merchant(LCA&DP)
The merchant Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6864 Accepted: 2375 Desc ...
- [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)
BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...
- 抓住那只牛!Catch That Cow POJ-3278 BFS
题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- 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 ...
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
随机推荐
- POJ 3688 Cheat in the Game(博弈论)
[题目链接] http://poj.org/problem?id=3688 [题目大意] 有俩人玩一个取石子的游戏,你是裁判. 游戏中有W块石头和N张卡片,卡片上分别写着数字Ai. 玩家随机抽走一张卡 ...
- 【动态规划】bzoj1633 [Usaco2007 Feb]The Cow Lexicon 牛的词典
f[i]=min{f[i+1]+1,f[i+len[j]+cant]+cant}(for i=L-1 downto 0)(1<=j<=w) #include<cstdio> # ...
- iOS使用CoreData实现收藏功能
一般做收藏都是使用数据库或者归档,使用CoreData实现收藏功能就是没事时练一下,实现大概和数据库差不多. 首先创建一个工具类继承NSObject,在里面实现所需要的方法. 工具类的.h文件: ty ...
- GNS3 思科模块 说明
GNS3整合了如下的软件: Dynamips :一款可以让用户直接运行Cisco系统(IOS)的模拟器 Dynagen :是Dynamips的文字显示前端 Pemu : PIX防火墙设备模 ...
- Matlab设置Legend横排、分块
高级用法1:指定legend显示的位置: legend({'str1','str2','strn'},'Location','SouthEast'); 比较鸡肋,画好图后树手动拖动就好了 高级用法2: ...
- Word中对象显示不完整
选中上下文字后,右键没有段落,如果是图片的话是有的,那么我们可以点击菜单栏中段落的右下三角,在那设置单倍行距.
- Vmware+Virtualbox+Ubuntu+debian+USB转串口+kermit
当前的环境是:在Win7笔记本主机上安装VirtualBox+Ubuntu12_04,串口使用USB转串口 如果使用的虚拟机是VirtualBox: 如果使用的虚拟机是Vmware: 执行这步后,主机 ...
- jquery--动态篇
jQuery中隐藏元素的hide方法 让页面上的元素不可见,一般可以通过设置css的display为none属性.但是通过css直接修改是静态的布局,如果在代码执行的时候,一般是通过js控制元素的st ...
- jquery ui autocomplete
//条码录入,自动完成功能 function addAutoComplete() { $('#txt_spuNo').autocomplete({ autoFocus: true, source: f ...
- 如何看待Linux操作系统的用户空间和内核空间
作为中央核心处理单元的CPU,除了生产工艺的不断革新进步外,在处理数据和响应速度方面也需要有权衡.稍有微机原理基础的人都知道Intel X86体系的CPU提供了四种特权模式ring0~ring3,其中 ...