poj3278:http://poj.org/problem?id=3278

题意:给你一个n和k,n可以加1也可以减1,还可以乘2,现在要求n经过这样的几步变换可以使得n==k;求得最小的步数。
题解:一开始我也不知道怎么办,准备用深度优先搜,但是自己没打出来,后来看了网上题目归类是BFSBFS,马上懂了,就是一个3入口的BFS。裸题。不过要注意0*2的情况。

  1. #include<cstring>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<iostream>
  5. #include<queue>
  6. using namespace std;
  7. int n,k;
  8. int counts[];
  9. struct Node {
  10. int x;
  11. int step;
  12. };
  13. int BFS(int x){
  14. for(int i=;i<=;i++)
  15. counts[i]=;
  16. counts[x]=;
  17. queue<Node>Q;
  18. Node tt;
  19. tt.x=x;
  20. tt.step=;
  21. Q.push(tt);
  22. while(!Q.empty()){
  23. Node temp=Q.front();
  24. Q.pop();
  25. int xx=temp.x;
  26. int step=temp.step;
  27. if(xx+<=&&step+<counts[xx+]){
  28. counts[xx+]=step+;
  29. Node ttt;
  30. ttt.x=xx+;
  31. ttt.step=step+;
  32. Q.push(ttt);
  33. }
  34. if(xx->=&&step+<counts[xx-]){
  35. counts[xx-]=step+;
  36. Node ttt;
  37. ttt.x=xx-;
  38. ttt.step=step+;
  39. Q.push(ttt);
  40. }
  41. if(xx!=&&*xx<=&&step+<counts[xx*]){//注意这里的xx不等于0的判断
  42. counts[xx*]=step+;
  43. Node ttt;
  44. ttt.x=xx*;
  45. ttt.step=step+;
  46. Q.push(ttt);
  47. }
  48. }
  49. return counts[k];
  50. }
  51. int main(){
  52. scanf("%d%d",&n,&k);
  53. printf("%d\n",BFS(n));
  54.  
  55. }

Catch That Cow的更多相关文章

  1. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  2. poj3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 73973   Accepted: 23308 ...

  3. catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38263   Accepted: 11891 ...

  4. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  5. 2016HUAS暑假集训训练题 B - Catch That Cow

    B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...

  6. 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 ...

  7. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

  8. Catch That Cow 分类: POJ 2015-06-29 19:06 10人阅读 评论(0) 收藏

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 58072   Accepted: 18061 ...

  9. [HDOJ2717]Catch That Cow

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  10. 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,最先 ...

随机推荐

  1. Linux Resin 安装

    1 Resin 下载 Resin 官方下载网址. 最新版下载 resin-4.0.36.tar.gz(免费版) resin 安装须要提前配置好jdk.配置jdk请看上面文章 2 Resin 安装 (1 ...

  2. [RxJS] Filtering operators: takeLast, last

    Operators take(), skip(), and first() all refer to values emitted in the beginning of an Observable ...

  3. hdoj Last non-zero Digit in N! 【数论】

    找规律! 求N!最后非0位的值.比方2是120的最后一个不是0的值. 输入N比較大,要大数保存. 注意到最后0的个数是与5的因数的个数相等.设f(n)为n!的最后非0位. 那么f(n)=((n%5)! ...

  4. Objective-C中的@Property详解

    Objective-C中的@Property详解 @Property (属性) class vairs 这个属性有nonatomic, strong, weak, retain, copy等等 我把它 ...

  5. 关于Sublime Text3 pyV8无法加载的问题

    昨天切换到sublime text 3  安装 emmet插件 不起作用  提示  pyv8 无法加载 手动下载安装解决 问题描述 PyV8 Binaries Archive of pre-compi ...

  6. CentOS 通过yum来升级php到php5.6,yum upgrade php 没有更新包怎么办?

    在文章中,我们将展示在centOS系统下如何将php升级到5.6,之前通过yum来安装lamp环境,直接升级的话,提示没有更新包,也就是说默认情况下php5.3.3是最新 1.查看已经安装的php版本 ...

  7. Conversion Between DataTable and List in C#

    1.List to DataTable public static DataTable ToDataTable<TSource>(this IList<TSource> dat ...

  8. jquery ajax异步调用

    写程序的第一步都要知其然,至于知其所以然就要看个人的爱好了.下面说一下web开发中经常用的ajax. 这里是用的jquery框架实现的ajax异步调用.废话少说先上代码.(asp.net开发) var ...

  9. Android常用组件【转】

    UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ...

  10. C# 数字证书微信API调用使用参考事例

    X.509 v.3 证书的方法.一个比较完整的调用  微信  API的示例: private stringGetResponseResult()         { string strRespons ...