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不能超过最大值. ...
随机推荐
- minio上传大于30M文件失败
minio上传30M文件失败.提示失败或者进度不动.翻查手册发现并未限速.原因是使用了nginx做代理.上传超过30M大的客户端文件无法正常上传,修改了下nginx的配置,就可以了. 加上client ...
- 转--python -- 收发邮件
官方 import smtplib from email.mime.text import MIMEText from email.header import Header # 发送邮箱服务器 smt ...
- spring boot(十):定时任务
springboot默认已经帮我们实行了,只需要添加相应的注解就可以实现. 1.pom包配置 <dependency> <groupId>org.springframework ...
- hdp3: regionserver running as process 3170. Stop it first.
1 启动hbase报错 hdp3: regionserver running as process 3170. Stop it first. 2 始终没有发现错误日志 3 jps没有发现hbase的r ...
- IO流总结笔记二
Reader |--InputStreamReader |--FileReader:专门用于处理文件的字符读取流对象 Writer |--OutputStreamWriter |--FileWri ...
- Junit的Assert用法
package junit.framework; /** * A set of assert methods. Messages are only displayed when an assert f ...
- ubuntu16.04+caffe+python接口配置
在Windows上用了一个学期的caffe了.深感各种不便,于是乎这几天在ubuntu上配置了caffe和它的python接口,现在记录配置过程,亲测可用: 环境:ubuntu16.04 , caff ...
- ARMV8 datasheet学习笔记4:AArch64系统级体系结构之编程模型(3)- 异常
1.前言 本文介绍异常相关内容,包括异常类型,异常进入,异常返回,异常层次结构,异常的路由等 2. RESET ARMV8体系结构支持两种类型的RESET Cold reset:Reset PE所有 ...
- UML和模式应用4:初始阶段(1)--概述
1.前言 UP开发包括四个阶段:初始阶段.细化阶段.构建阶段.移交阶段: 初始阶段是项目比较简短的起始步骤,主要目的是收集足够的信息来建立共同设想,调查项目的总体目标和可行性,确定是否值得进一步深入. ...
- 用secureCRT操作ubuntu终端
用secureCRT操作ubuntu终端 ubuntu下先安装ssh windows下win+R再输入ubuntu的ip地址 ubuntu 检测端口号的命令 netstat -antp 下载到 ...