HDU 2717(* bfs)
题意是在一个数轴上,每次可以一步到达当前位置数值的 2 倍的位置或者数值 +1 或数值 -1 的位置,给定 n 和 k,问从数值为 n 的位置最少多少步可以到达数值为 k 的位置。
用广搜的方法,把已经到达的位置标记,检查三个方向(*2,+1,-1)的位置是否到达 k,如已经到达就返回遍历的层数,否则将新的位置标记,继续检查新位置的三个方向。
这题要注意剪枝,每次的新位置要大于 0 小于 1e6,且当 n 大于 k 的时候要特殊处理,此时只能通过 -1 的方法到达 k,故输出 n - k 即可。
代码如下:
#include <bits/stdc++.h>
using namespace std; int n,k,cnt,a[];
bool vis[];
int bfs(int from,int times)
{ int ccnt = cnt;
for(int i = from; i < ccnt; ++i)
{
if(a[i]*==k || a[i]+==k || a[i]-==k)
return ++times;
else
{
if(a[i]*<=&&(!vis[a[i]*]))
{
a[cnt++] = a[i]*;
vis[a[i]*] = ;
}
if(a[i]->=&&(!vis[a[i]-]))
{
a[cnt++] = a[i]-;
vis[a[i]-] = ;
}
if(a[i]+<=&&(!vis[a[i]+]))
{
a[cnt++] = a[i]+;
vis[a[i]+] = ;
}
}
}
return bfs(ccnt,++times);
}
int main()
{
while(~scanf("%d %d",&n,&k))
{
if(n==k)
puts("");
else if(n>k)
printf("%d\n",n-k);
else
{
cnt = ;
a[] = n;
memset(vis,,sizeof(vis));
vis[n] = ;
printf("%d\n",bfs(,));
}
}
return ;
}
HDU 2717(* 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 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- 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 ...
- 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)
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- hdu 2717 Catch That Cow(BFS,剪枝)
题目 #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> ...
- hdu 2717 从n点走到k点 (BFS)
在横坐标上 从n点走到k点 至少要几步 可以到 n+1 n-1 n*2这3个点 Sample Input5 17 Sample Output4 #include <iostream> #i ...
- 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(广搜bfs)
题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...
随机推荐
- Android探究之Gson@SerializedName
@SerializedName注解的意义 当我们使用Gson解析Json数据时都会创建一个对应实体类,有时候Json数据里面的字段是Java关键词或者Json数据里面的字段太简单,我们想在实体类中自定 ...
- DbGridEh根据某一个字段的值显示对应底色或字体变化
改变行底色: procedure TForm1.dggrideh1DrawColumnCell(Sender: TObject;const Rect: TRect; DataCol: Integer; ...
- ORA-02266错误的批量生成脚本解决方案
ORA-02266: unique/primary keys in table referenced by enabled foreign keys这篇博客是很早之前总结的一篇文章,最近导数时使用TR ...
- IKAnalyzer结合Lucene实现中文分词
1.基本介绍 随着分词在信息检索领域应用的越来越广泛,分词这门技术对大家并不陌生.对于英文分词处理相对简单,经过拆分单词.排斥停止词.提取词干的过程基本就能实现英文分词,单对于中文分词而言,由于语义的 ...
- Java中数组和集合的foreach操作编译后究竟是啥
今天和同事在关于foreach编译后是for循环还是迭代器有了不同意见,特做了个Demo,了解一下. 是啥自己来看吧! public class Demo { public static void m ...
- JS 设计模式七 -- 外观模式
概念 为子系统中的一组接口提供一个一致的界面,定义一个高层接口,这个接口使子系统更加容易使用. 外观模式在JS中,可以认为是一组函数的集合. 实现 // 三个处理函数 function start() ...
- 高橋君とカード / Tak and Cards AtCoder - 2037 (DP)
Problem Statement Tak has N cards. On the i-th (1≤i≤N) card is written an integer xi. He is selectin ...
- java基础-04泛型
介绍 泛型就是数据类型的参数化表示,泛型的本质是参数化类型,常用E代表任何数据类型,在实际使用的时候把实际的数据类型传递给E. 泛型的好处是设计通用的功能,多个数据类型可以共用. 泛型类型E只能代表O ...
- js 简单弹框toast
新建toast.js文件 function Toast(msg,duration){ duration=isNaN(duration)?3000:duration; var m = document. ...
- Cnario Player 接入视频采集卡采集外部音视频信号测试
测试产品 型号: TC-D56N1-30P采集卡 参数: 1* HDMI 1.4输入, PCIe 接口为PCI-Express x4(Gen2), 最高支持4096x2160@30Hz, 支持1920 ...