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不能超过最大值. ...
随机推荐
- Nginx管理脚本
#!/bin/bash # chkconfig: # description: Start/Stop Nginx server path=/application/nginx/sbin pid=/ap ...
- SQL语句——重复记录
1.查找重复记录: (按id查找) select * from user_info where id in ( select id from user_info group by id ) 即:sel ...
- Failed to load or instantiate TagLibraryValidator class: org.apache.taglibs.standard.tlv.JstlCoreTLV
今天在使用JSP,引入<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> ...
- AES加解密算法
直接粘代码,该类是基于微信公众号消息加密解密所提供的PHP DEMO改造而来,目前使用于彬彬大学APP接口token校验中. php的mcrypt 扩展已经过时了大约10年,并且用起来很复杂.因此它被 ...
- split('\r\n')
'\r'是回车,'\n'是换行,前者使光标到行首,后者使光标下移一格.通常用的Enter是两个加起来. 实际我的脚本读取FTP的列表,如果用的split("\r\n"),可以获得正 ...
- D - Searching the String (AC自动机)
题目链接:https://cn.vjudge.net/contest/281961#problem/D 题目大意:给你一个模式串,然后给你多个匹配串,匹配串的类型是包括可以覆盖的以及不可覆盖的. 具体 ...
- jquery 学习(七) - 常用动态效果
<!--转载于 听说你的代码很6--><!--http://www.jq22.com/webqd2377--> CSS <style> #content #firs ...
- Java打印M图形(二维数组)——(九)
对于平面图形输出集合图形与数字组合的,用二维数组.先在Excel表格中分析一下,找到简单的规律.二维数组的行数为行高,列数为最后一个数大小. 对于减小再增大再减小再增大的,可以用一个boolean标志 ...
- linux笔记_day04
1.cat 连接并显示 -n 显示行号 -E END 显示行尾 2.tac 从后往前显示 3.ctrl +C 4.more 向后翻 到最后会退出 5.less 翻到最后不退出 常用 支持b k sp ...
- ubuntu 14.04 安装 OpenCV -2.4.13
1. 安装 (1) 更新软件源 sudo apt-get update sudo apt-get upgrade (2)删除以前安装的 FFMPEG 和 x264 库: sudo apt-get re ...