POJ_3278_Catch That Cow
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 54911 | Accepted: 17176 |
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<queue>
#include<cstring>
using namespace std;
#define N 100005
struct Node
{
int x;
int step;
};
queue<struct Node>q;
bool visit[N]; bool ok(int x)
{
if(x>= && x<=)
return ;
return ;
} int cal(int i,int x)
{
if(i==)
return x+;
else if(i==)
return x-;
else
return x*;
}
void bfs(int a,int b)
{
memset(visit,,sizeof(visit));
while(!q.empty())
{
q.pop();
}
Node aa;
aa.step=;
aa.x=a;
q.push(aa);
visit[aa.x]=;
while(!q.empty())
{
Node tmp=q.front();
q.pop();
for(int i=; i<; i++)
{
int y=cal(i,tmp.x);
if(ok(y)&&!visit[y])
{
if(y==b)
cout<<tmp.step<<endl;
else
{
aa.step=tmp.step+;
aa.x=y;
q.push(aa); }
visit[y]=;
} }
}
}
int main()
{
int n,k;
cin>>n>>k;
if(k>n)
bfs(n,k);
else
cout<<(n-k);
return ;
}
POJ_3278_Catch That Cow的更多相关文章
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心
SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...
- HDU Cow Sorting (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1 ...
- [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
[BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 试题描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发 ...
- 细读cow.osg
细读cow.osg 转自:http://www.cnblogs.com/mumuliang/archive/2010/06/03/1873543.html 对,就是那只著名的奶牛. //Group节点 ...
- POJ 3176 Cow Bowling
Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13016 Accepted: 8598 Desc ...
- raw,cow,qcow,qcow2镜像的比较
在linux下,虚拟机的选择方式有很多,比如vmware for linux,virtual box,还有qemu,在以前,使用qemu的人不多,主要是使用起来有些麻烦,但现在随着Openstack的 ...
- poj1985 Cow Marathon (求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 3195 Accepted: 1596 Case ...
- (01背包变形) Cow Exhibition (poj 2184)
http://poj.org/problem?id=2184 Description "Fat and docile, big and dumb, they look so stupid ...
随机推荐
- JS 获取form表单的所有数据
在HTML中用js获取通过GET.POST方法(就是在网址后加?a=b&c=d之类)传过来的表单值. 针对大家常用的获取表单方式,很多时候都是在重复的写一些代码,今天给大家贴出来的代码可以作为 ...
- php把时间计算成几分钟前,几小时前,几天前的函数
function time_tran($the_time){ $now_time = date("Y-m-d H:i:s",time()+8*60*60); $now_time = ...
- SEO 搜索引擎优化培训01
百度搜索风云榜:http://top.baidu.com/boards 页面上的因素:对搜索引擎而言
- python开发【第4篇】【进程、线程、协程】
一.进程与线程概述: 进程,是并发执行的程序在执行过程中分配和管理资源的基本单位,每一个进程都有一个自己的地址空 间. 线程,是进程的一部分,一个没有线程的进程可以被看作是单线程的.线程有时又被称为轻 ...
- dumpdecrypted进行砸壳
1.下载源码git clone git://github.com/stefanesser/dumpdecrypted/ 2.进行编译生成 dumpdecrypted.dylibmake 3.将dump ...
- [计算机]如何在win7下查看并更改文件的默认后缀名
如何在win7下查看默认文件的后缀名并更改呢? 例如有一个文件本来是exe,想变更为txt.但是无法看到后缀名,就无法更改. 双击桌面上的计算机图标,或者任意盘符界面,单击如下图左侧“组织”右侧的下拉 ...
- easyUI的treegrid添加节点(append)时间过长,设置等待(wait)遮罩效果
如题所述,在treegrid中,一次添加多个节点时,时间很长,但easyUI并无显示等待信息,让人以为是陷入了死循环,或者死机了,用户体验很差. 本来,treegrid(或者datagrid)有所谓的 ...
- HTML DOM createTextNode() 方法
实例 创建一个文本节点: var btn=document.createTextNode("Hello World"); 输出结果: Hello World 尝试一下 » HTML ...
- mysql_connect 等待时间长,修改连接地址为127.0.0.1即可
程序搬家后,运行非常慢分析流程: 1.外网的等待时间太长 2.内容等待时间也很长 3.断点查到仅仅一句 mysql_connect ("localhost",***,***)就要1 ...
- Hibernate- Criteria 简易
package cn.demo; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Session; ...