hdu 2717 从n点走到k点 (BFS)
在横坐标上 从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)的更多相关文章
- 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,最先 ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 2717 Catch That Cow (深搜)
题目链接 Problem Description Farmer John has been informed of the location of a fugitive cow and wants t ...
- 杭电 HDU 2717 Catch That Cow
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 1253:胜利大逃亡(基础广搜BFS)
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu 2157 从a点走到b点刚好k步的方案数是多少 (矩阵快速幂)
n个点 m条路 询问T次 从a点走到b点刚好k步的方案数是多少 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值把 给定的图转为邻接矩阵,即A(i,j)=1当且仅当存 ...
- 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 ...
- 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 ...
- HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............
看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717 思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值. ...
随机推荐
- CentOS6.8搭建rabbitmq消息中间件
参考资料:http://blog.csdn.net/yunfeng482/article/details/72853983 一.rabbitmq简介 MQ全称为Message Queue, 消息队列( ...
- XML文件详解以及解析
转自:https://blog.csdn.net/com_ma/article/details/73277535 一.xml基础详解: 1.概述: xml:即可扩展标记语言,xml是互联网数据传输的重 ...
- HITS算法--从原理到实现
本文介绍HITS算法的相关内容. 1.算法来源 2.算法原理 3.算法证明 4.算法实现 4.1 基于迭代法的简单实现 4.2 MapReduce实现 5.HITS算法的缺点 6.写在最后 参考资料 ...
- 关闭MySQL数据库的几种方法
#1.使用mysqldadmin mysqladmin -uroot -p shutdown mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/$ ...
- Java入门系列(十)Java IO
概述 总体而言,java的读写操作又分为两种:字符流和字节流. 实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作文件. 什么是流? ...
- FastReport使用List的方法
public class User { string username; string password; public User(string username, string password) ...
- 作业引擎quartz.net --- 监听链
针对多个作业:如何描述各个跑批任务之间的顺序,紧前.紧后关系,实现灵活调度.例如:A完成则B开始,B完成C开始. 对quartz.net 进行了查阅,能实现如上业务,如下图: 测试代码: using ...
- spfa判负环
bfs版spfa void spfa(){ queue<int> q; ;i<=n;i++) dis[i]=inf; q.push();dis[]=;vis[]=; while(!q ...
- luogu P4036 [JSOI2008]火星人
传送门 很久以前xzz大佬就喊我做这题,结果现在才做qwq 因为要在序列中插入,所以直接用\(Splay\)维护这个串的哈希值,插入就直接把那个点插♂进去,修改就把点旋到根,然后修改和pushup,询 ...
- NOIP2018TG题解
应该不会被禁赛吧qwq Day1 T1 铺设道路 €€£:我 抄 我 自 己 我考场上写的是一个类似于差分的做法,就是开个数组表示某个位置是否贡献答案,从小到大枚举高度,那么一个位置能贡献答案当且仅当 ...