在横坐标上 从n点走到k点 至少要几步 可以到 n+1 n-1 n*2这3个点

Sample Input
5 17

Sample Output
4

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std; int n , k ;
int ans ;
bool v[] ; struct node
{
int x ;
int step ;
}; bool yes(int x)
{
if (x>= && x<= && v[x] == )
return ;
else
return ;
} int bfs()
{
queue<node> q ;
node now , t ;
now.x = n ;
now.step = ;
q.push(now) ;
memset(v , , sizeof(v)) ;
v[n] = ;
while(!q.empty())
{
now = q.front() ;
q.pop() ;
if (now.x == k)
return now.step ;
t.x = now.x + ;
if (yes(t.x))
{
t.step = now.step + ;
q.push(t) ;
v[t.x] = ;
}
t.x = now.x - ;
if (yes(t.x))
{
t.step = now.step + ;
q.push(t) ;
v[t.x] = ;
}
t.x = now.x * ;
if (yes(t.x))
{
t.step = now.step + ;
q.push(t) ;
v[t.x] = ;
} }
return ;
} int main ()
{ while (scanf("%d %d" , &n , &k) != EOF)
{ ans = bfs() ;
printf("%d\n" , ans) ;
} return ;
}

hdu 2717 从n点走到k点 (BFS)的更多相关文章

  1. 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,最先 ...

  2. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. HDU 2717 Catch That Cow (深搜)

    题目链接 Problem Description Farmer John has been informed of the location of a fugitive cow and wants t ...

  4. 杭电 HDU 2717 Catch That Cow

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  5. hdu 1253:胜利大逃亡(基础广搜BFS)

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  6. hdu 2157 从a点走到b点刚好k步的方案数是多少 (矩阵快速幂)

    n个点 m条路 询问T次 从a点走到b点刚好k步的方案数是多少 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值把 给定的图转为邻接矩阵,即A(i,j)=1当且仅当存 ...

  7. HDU 2717 Catch That Cow(常规bfs)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...

  8. 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 ...

  9. HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............

    看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717 思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值. ...

随机推荐

  1. charCodeAt() 和charAt()

    charAt() 方法可返回指定位置的字符. charCodeAt() 方法可返回指定位置的字符的 Unicode 编码.这个返回值是 0 - 65535 之间的整数. 方法 charCodeAt() ...

  2. [C++]线性链表之顺序表<二>

    /*   @content 线性链表之顺序表   @date 2017-3-21 1:06   @author Johnny Zen  */ /* 线性表     顺序表     链式表[带头指针/不 ...

  3. JavaScript之BOM五大对象(window;location;navigator;screen;history)

    一.window 1.浏览器窗口与页面各部分尺寸 2.间歇调用与超时调用 setInterval(function,time(以毫秒计)) 返回:时间Id号(为clearInterval(timeId ...

  4. rem,em

    任意浏览器的默认字体高都是16px.所有未经调整的浏览器都符合: 1em=16px.那么12px=0.75em,10px=0.625em.为了简化font-size的换算,需要在css中的body选择 ...

  5. B. Planning The Expedition

    题目链接:http://codeforces.com/contest/1011/problem/B 题目大意: 输入的n,m代表n个人,m个包裹. 标准就是 每个人一开始只能选定吃哪一个包裹里的食物, ...

  6. geeksforgeeks-Array-Rotation and deletion

      As usual Babul is again back with his problem and now with numbers. He thought of an array of numb ...

  7. POI导出Excel(xls、xlsx均可以,也支持图片)——(三)

    Jar包

  8. DMA内存申请--dma_alloc_coherent 及 寄存器与内存【转】

    转自:https://blog.csdn.net/ic_soc_arm_robin/article/details/8203933 在项目驱动过程中会经常用到dma传输数据,而dma需要的内存有自己的 ...

  9. php 日期格式转换万能公式

    思路用strtotime转换时间的字符串 $t='2017-03-09 02:30'; echo(date('Y-m-d H-i', strtotime($t)));

  10. springboot系列八、springboot整合kafka

    背景: 当业务在同一时间出现高并发的时候,这个时候我们不想无限的增加服务器,但是又想提高吞吐量.这时可以考虑使用消息异步处理,进行消峰填谷:同时还可以降低耦合度.常见的消息中间件有kafka,rabb ...