Catch That Cow(bfs)
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
此题需要注意的是queue应该定义全局,这一点上我wa了好几发
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{
int cur,step;
}st;
queue<node>q;
int m,n,ans,visit[];
int dfs(int x)
{
int i;
while(!q.empty())
{
q.pop();
}
memset(visit,,sizeof(visit));
node s,p;
q.push(st);
visit[st.cur]=;
while(!q.empty())
{
p=q.front();
q.pop();
if(p.cur==n)
return p.step;
for(i=;i<=;i++)
{
s=p;
if(i==)
s.cur-=;
else if(i==)
s.cur+=;
else
s.cur*=;
s.step++;
if(s.cur==n)
return s.step;
if(s.cur>=&&s.cur<=&&!visit[s.cur])
{
visit[s.cur]=;
q.push(s);
} }
} }
int main()
{
while(scanf("%d%d",&m,&n)!=-)
{
st.cur=m;
st.step=;
ans=dfs(m);
printf("%d\n",ans);
}
return ;
}
Catch That Cow(bfs)的更多相关文章
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- HDU 2717 Catch That Cow(BFS)
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- Catch That Cow(BFS)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- ***参考Catch That Cow(BFS)
Catch That Cow Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- Catch That Cow (bfs)
Catch That Cow bfs代码 #include<cstdio> #include<cstring> #include<algorithm> #inclu ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 【OpenJ_Bailian - 4001】 Catch That Cow(bfs+优先队列)
Catch That Cow Descriptions: Farmer John has been informed of the location of a fugitive cow and wan ...
- poj 3278(hdu 2717) Catch That Cow(bfs)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- POJ3279 Catch That Cow(BFS)
本文来源于:http://blog.csdn.net/svitter 意甲冠军:给你一个数字n, 一个数字k.分别代表主人的位置和奶牛的位置,主任能够移动的方案有x+1, x-1, 2*x.求主人找到 ...
随机推荐
- LeetCode OJ:Set Matrix Zeroes(设置矩阵0元素)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 这题要注意的 ...
- Android中UI线程与后台线程交互设计的6种方法
在android的设计思想中,为了确保用户顺滑的操作体验.一些耗时的任务不能够在UI线程中运行,像访问网络就属于这类任务.因此我们必须要重新开启 一个后台线程运行这些任务.然而,往往这些任务最终又会直 ...
- MySqlDBHelper数据库连接
这里是本人在工作中用到,希望给大家帮助 public class MySqlDBHelper { //获取一个记录器 private static readonly log4net.ILog log ...
- 一个css3 DNA 效果
这个效果就是 不断沿 Y 轴旋转 <div id="container"></div> <style> body{ background:bla ...
- Leetcode 890. Find and Replace Pattern
把pattern映射到数字,也就是把pattern标准化. 比如abb和cdd如果都能标准化为011,那么就是同构的. class Solution: def findAndReplacePatter ...
- ranch实现游戏服务器
在 erlang游戏开发tcp 我们建立起了自己的socket tcp 服务器的基本骨架.当时面对并发情况下,多人同一时刻连接服务器的时候,我们的基本骨架 还是难以应付处理.这就使我不得不想对这样的情 ...
- python之random库
random库是用于产生并运用随机数的标准库 1. random库函数 (1)random.seed(a) 设置随机种子数,可以是浮点数或整数,如果不设置的话,则random库默认以系统时间产生当作随 ...
- map/reduce类简单介绍
在Hadoop的mapper类中,有4个主要的函数,分别是:setup,clearup,map,run.代码如下: protected void setup(Context context) thro ...
- shh整合后web.xml、spring配置文件和struts.xml的内容
1:web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=" ...
- window.top.location
window.top.location的作用 top, 表示是顶层页面, 因为页面之中可能嵌入了 frame 等子页面,top表示最外面一层 Html代码 <html> <hea ...