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不能超过最大值. ...
随机推荐
- Calendar add 方法 和set方法
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c = Calendar. ...
- Gym - 100269F Flight Boarding Optimization(dp+树状数组)
原题链接 题意: 现在有n个人,s个位置和你可以划分长k个区域你可以把s个位置划分成k个区域,这样每个人坐下你的代价是该区域内,在你之前比你小的人的数量问你怎么划分这s个位置(当然,每个区域必须是连续 ...
- wireshark数据包分析
最近有不少同事开始学习Wireshark,他们遇到的第一个困难就是理解不了主界面上的提示信息,于是跑来问我.问的人多了,我也总结成一篇文章,希望对大家有所帮助.Wireshark的提示可是其最有价值之 ...
- Java SE之String,字符串和子字符串的存储与区别
理解String 是怎么占用内存的 来看一个每个String对象的各个属性,一个String包括如下的属性: 一个char数组(是个独立的对象用来存储字符串中的字符) 一个int 的off ...
- c# 匿名函数
int t(){ Func<int> m=()=>3; return m()+m();}
- POJ 2407 Relatives (欧拉函数)
题目链接 Description Given n, a positive integer, how many positive integers less than n are relatively ...
- transform,变换
1.transform属性:rotate(翻转),skew(倾斜),scale(缩放),translate(移位) 用法:transform: rotate(45deg) scale(0.5) ske ...
- python队列queue 之优先级队列
import queue as Q def PriorityQueue_int(): que = Q.PriorityQueue() que.put(10) que.put(1) que.put(5) ...
- 以前的 Delphi版本
Delphi 1 Delphi 2 Delphi 3 Delphi 4 Delphi 5 Delphi 6 Delphi 7 Delphi 8 Delphi 2005
- select 1
select 1 from mytable;与select anycol(目的表集合中的任意一行) from mytable;与select * from mytable 作用上来说是没有差别的,都是 ...