POJ3278Catch That Cow(BFS)
Catch That Cow
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 70652 | Accepted: 22216 |
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
/*
*题意:有一个农民和一头牛,他们在一个数轴上,牛在k位置保持不动,农户开始时在n位置。
*设农户当前在M位置,每次移动时有三种选择:
*1.移动到M-1;
*2.移动到M+1位置;
*3.移动到M*2的位置。
*问最少移动多少次可以移动到牛所在的位置。所以可以用广搜来搜索这三个状态,直到搜索到牛所在的位置。
*
*/#include<cstdio>
#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 vis[N];void BFS()
{ //搜索关于X的三种状态int X, STEP;while(!Q.empty()){node tmp = Q.front();Q.pop();X = tmp.x;STEP = tmp.step;if(X == k){printf("%d\n",STEP);return ;}if(X >= 1 && !vis[X - 1]) //要保证减1后有意义,所以要X >= 1{node temp;vis[X - 1] = 1;temp.x = X - 1;temp.step = STEP + 1;Q.push(temp);}if(X <= k && !vis[X + 1]){node temp;vis[X + 1] = 1;temp.x = X + 1;temp.step = STEP + 1;Q.push(temp);}if(X <= k && !vis[X * 2]){node temp;vis[X * 2] = 1;temp.x = 2 * X;temp.step = STEP + 1;Q.push(temp);}}
}int main()
{while(~scanf("%d%d",&n,&k)){while(!Q.empty()) Q.pop();memset(vis,0,sizeof(vis));vis[n] = 1;node t;t.x = n, t.step = 0;Q.push(t);BFS();}return 0;
}
POJ3278Catch That Cow(BFS)相关推荐
- POJ 3278 Catch That Cow(BFS)
题目网址:http://poj.org/problem?id=3278 题目: Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Tot ...
- hdoj2717Catch That Cow(BFS)
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- HDU 2717 Catch That Cow(BFS)
题目链接 好裸,BFS.杭电多组..2A.. 1 #include <stdio.h> 2 #include <string.h> 3 int p[100001],o[1000 ...
- C - Catch That Cow(BFS)
找奶牛 有三种移动方式,作为BFS搜索的方向 利用 当 n > k 时只能向后走,优化结果 #include<iostream> #include<cstring> #i ...
- poj 3278 Catch That Cow (bfs)
题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 1 #include< ...
- HDU-2717-Catch That Cow(bfs)
Catch That Cow Problem Description Farmer John has been infor ...
- HDU2717 Catch That Cow(bfs)
题意:农夫的牛跑了,给出农夫和牛在坐标轴上的位置n和k,农夫每次只能从点n移动到n-1.n+1或者n*2的位置.输出抓到牛所需要的最小移动次数. 思路:思路明显的bfs,每次搜索只按照这三种方式,标记 ...
- 搜索 —— 广度优先搜索(BFS)
[概述] 广度优先搜索从初始状态 S 开始,利用给定的规则,生成当前状态所有可能的状态,构成的下一层节点,检查是否出现目标状态G,若未出现,就对该层所有状态节点,分别顺序利用规则再次生成再下一层的所有 ...
- 广度优先算法(BFS)入門理解
简介 广度优先算法(BFS)和深度优先算法(DFS)是图这种数据结构最基础的算法,所以需要我们花心思去学习.能知道BFS和DFS这个名词就证明同学们已经在书上看到了他们的介绍.不过对于初入门的同学来说 ...
最新文章
- Facebook又放大招!开源框架Pythia让深度学习更高效
- 【码云周刊第 68 期】数据可视化:商业智能的未来!
- 不可思议的Word2Vec系列一数学原理
- “AI+”赋能元宇宙,一文探讨智能交互的技术支撑
- 互联网晚报 | 3月2日 星期三 |​ ​最高法:电商不得以商品已拆封为由拒绝七日无理由退货;小米投资纽迪瑞...
- 10.2829(NOIP模拟修正总结)
- C++socket编程(七):7.3 http的响应协议
- 苹果开发者账户需要同意并添加电话号码,苹果账号忘记验证问题解决方案
- json 插入数据_MongoDB如何一次插入多条json数据
- mysql 版本_mysql各个版本介绍
- java jdk 8 中文文档
- window.dialogArguments.location.reload();
- linux 安装pgadmin4
- 经济机器是如何运行的
- 初出茅庐的小李第23篇博客之WiFi模块建立TCP通信并获取B站粉丝数据
- 字符编码常识及问题解析
- (七)通过pygame来设置飞机大战中 敌机 的速度、位置等
- 瞧瞧你的指甲,看你身体还好不!
- 一个基于EntityFrameworkCore+Lucene实现的全文搜索引擎库
- Day9--MATLAB常用绘图命令