poj3278Catch That Cow(BFS)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 37094 | Accepted: 11466 |
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
N and
K
Output
Sample Input
5 17
Sample Output
4
Hint
#include <queue>
#include <cstdio>
#include<cstring>
#define N 100001
using namespace std;
int n, k, ans;
bool vis[N];
struct node {
int local, time;
};
void bfs() {
int t, i;
node now, tmp;
queue<node> q;
now.local = n;
now.time = 0;
q.push(now);
memset(vis,false,sizeof(vis));
vis[now.local] = true;
while(!q.empty()) {
now = q.front();
q.pop();
for(i=0; i<3; i++) {
if(0==i) t = now.local-1;
else if(1==i) t = now.local+1;
else if(2==i) t = now.local*2;
if(t<0||t>N||vis[t]) continue;
if(t==k) {
ans = now.time+1;
return;
}
vis[t] = true;
tmp.local = t;
tmp.time = now.time+1;
q.push(tmp);
}
}
} int main() {
while(~scanf("%d%d",&n,&k)) {
if(n>=k) printf("%d\n",n-k);
else {
bfs();
printf("%d\n",ans);
}
}
}
poj3278Catch That Cow(BFS)的更多相关文章
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- poj3278-Catch That Cow 【bfs】
http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ3083Catch That Cow[BFS]
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 77420 Accepted: 24457 ...
- POJ3278——Catch That Cow(BFS)
Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...
- POJ3278 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搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- 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,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- Catch That Cow (BFS广搜)
问题描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...
随机推荐
- Android利用ViewFlipper实现屏幕切换动画效果
1.屏幕切换指的是在同一个Activity内屏幕见的切换,最长见的情况就是在一个FrameLayout内有多个页面,比如一个系统设置页面:一个个性化设置页面. 2.介绍ViewFilpper类 Vie ...
- C程序设计语言之一
%d 按照十进制整形数打印: %o 按照八进制整形数打印: %x 按照十六进制整形数打印: %c 表示字符 %s 表示字符串 %% 表示%本身打印: %ld long型输出 ”幻数“: #define ...
- ACE编译
(1)下载ACE6.1.0版本,路径如下 http://download.dre.vanderbilt.edu/previous_versions/ACE-6.1.0.tar.gz
- MyEclipse 引用其他项目及其jar包
倘若在工作区有两个项目A和B,B项目引用A项目及其jar包(防止调用时A项目的方法出现NoClassFound),步骤如下: 在A项目上点右键看属性,点击Build Path--->Concon ...
- Python 第十三篇之二:jQuery基础
一:jQuery是一个兼容多浏览器的javascript类库,核心理念是write less,do more(写得更少,做得更多),对javascript进行了封装,是的更加便捷的开发,并且在兼容性方 ...
- double 型变量的输入输出标准格式
c语言double型变量标准输入格式: scanf("%lf",num); 标准输出格式: printf("%f\n",num); 注:有过输出用%lf输出OJ ...
- JProtector 帮助文档
一.应用加密 1.使用 JProtector在线 进行应用加密:使用浏览器访问 http://app.shuton.net/encryptjar, 点击 Browse 选择待加密的应用jar包.war ...
- Java, C#, Swift语法对比速查表
原文:Java, C#, Swift语法对比速查表 Java 8 C# 6 Swift 变量 类型 变量名; 类型 变量名; var 变量名 : 类型; 变量(类型推断) N/A var 变量名= ...
- Android 蓝牙( Bluetooth)耳机连接分析及实现
Android 实现了对Headset 和Handsfree 两种profile 的支持.其实现核心是BluetoothHeadsetService,在PhoneApp 创建的时候会启动它. if ( ...
- 解决win7 中source insight没有courier new字节的问题
解决win7 中source insight没有courier new字节的问题 http://blog.csdn.net/season_hangzhou/article/details/18665 ...