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.求主人找到 ...
随机推荐
- Week04《Java程序设计》第四周学习总结
Week04<Java程序设计>第四周学习总结 1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 答:static关键字,final关键字,静态初始块,抽象类,继承, ...
- How to Have a Healthy Relationship --shanbei 为单身节写
我在扇贝发现一片好文. Sometimes relationships can seem like a lot of work until you sit back and realize just ...
- 简要谈谈javascript bind 方法
最近去参加了场面试,跟面试官聊了很多JS基础上的东西,其中有个问题是谈谈对apply.call.bind的理解和区别.顿时一愣,apply.call我知道,经常用的东西,bind是什么鬼!!!好像见过 ...
- js实现显示系统时间的表盘
核心: 1,document.styleSheets 修改css里的keyframes属性值 2,表针角度的计算 最终显示效果: <!DOCTYPE html> <html> ...
- Near Field Communication (NFC) applications
Near Field Communication (NFC) applications There has been little practical guidance available on NF ...
- Ubuntu 16.04 安装配置支持http2的nginx
第一步 安装最新版本的nginx 对于ubuntu16.04而言 直接装就是最新的 ``` sudo apt-get update sudo apt-get install nginx 查看Nginx ...
- InpOut32 InputTest.cpp hacking
/************************************************************************************ * InpOut32 Inp ...
- 【英语】Bingo口语笔记(81) - wear系列
- JAVA视频链接
Java基础Java马士兵:链接:https://pan.baidu.com/s/1jJRvxGi密码:v3xb Java刘意:链接:https://pan.baidu.com/s/1kVZQCqr密 ...
- .NET面试题总结
1.c#垃圾回收机制 从以下方面入手展开: 1.压缩合并算法 2.代的机制 3.GC调用终结器 2.委托和事件 先说它的定义:委托的本质是类,类型安全的指针,然后从用途上考虑,事件是包装的委托 ...