HDU 2717

  题目大意:在x坐标上,农夫在n,牛在k。农夫每次可以移动到n-1, n+1, n*2的点。求最少到达k的步数。

  思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先找到的一定是最小的步数。

  1. /* HDU 2717 Catch That Cow --- BFS */
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. using namespace std;
  6.  
  7. bool visit[];
  8. int n, k;
  9.  
  10. struct Node{
  11. int num;
  12. int step;
  13. Node(int lhs = , int rhs = ) :num(lhs), step(rhs){}
  14. };
  15.  
  16. inline bool judge(int x){
  17. if (x < || x > || visit[x])
  18. return ;
  19. return ;
  20. }
  21.  
  22. void bfs(){
  23. memset(visit, , sizeof visit);
  24. queue<Node> q;
  25. visit[n] = ; //标记起点已访问
  26. q.push(n);
  27.  
  28. while (!q.empty()){
  29. Node x = q.front(); q.pop();
  30. if (x.num == k){
  31. printf("%d\n", x.step);
  32. break;
  33. }
  34. Node y = x;
  35. ++y.step;
  36.  
  37. //第一个方向 x-1
  38. y.num = x.num - ;
  39. if (judge(y.num)){
  40. visit[y.num] = ; //标记该点已访问
  41. q.push(y);
  42. }
  43.  
  44. //第二个方向 x+1
  45. y.num = x.num + ;
  46. if (judge(y.num)){
  47. visit[y.num] = ; //标记该点已访问
  48. q.push(y);
  49. }
  50.  
  51. //第三个方向 2*x
  52. y.num = x.num * ;
  53. if (judge(y.num)){
  54. visit[y.num] = ; //标记该点已访问
  55. q.push(y);
  56. }
  57. }//while(q)
  58. }
  59.  
  60. int main()
  61. {
  62. while (scanf("%d%d", &n, &k) == ){
  63. bfs();
  64. }
  65. return ;
  66. }

HDU 2717 Catch That Cow --- BFS的更多相关文章

  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/Ot ...

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

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

  4. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

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

  5. hdu 2717 Catch That Cow(广搜bfs)

    题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...

  6. 杭电 HDU 2717 Catch That Cow

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

  7. 题解报告:hdu 2717 Catch That Cow(bfs)

    Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...

  8. hdu 2717 Catch That Cow(BFS,剪枝)

    题目 #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> ...

  9. HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............

    看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717 思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值. ...

随机推荐

  1. LightOJ 1047-Program C

    Description The people of Mohammadpur have decided to paint each of their houses red, green, or blue ...

  2. MJPhotoBrowser 两个bug:回到小图模式时会闪动&大图太靠近底部

    最近项目需要写网络的相片视频浏览的库, 没时间重写,使用了MJPhotoBrowser,里面的一些bug 和解决写在下面 1.-[MJPhotoLoadingView setProgress:]: m ...

  3. @ResultMapping注解

    @RequestMapping注解1.url映射放在方法上:@RequestMapping("/itemsEdit")2.窄化url请求映射放在类上,定义根路径,url就变成根路径 ...

  4. php的数组与数据结构

    一.数组的分类与定义 分类: 1.索引数组  $array = array(1,2,3,4,5); 2.关联数组  $array=array(1=>"aa","bb ...

  5. Android常见控件— — —TextView

    <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=&qu ...

  6. hdu4417 划分树+二分

    //Accepted 14796 KB 453 ms //划分树 //把查询的次数m打成n,也是醉了一晚上!!! //二分l--r区间第k大的数和h比较 #include <cstdio> ...

  7. hdu 1069

    //Accepted 264 KB 0 ms //每种block只有三种方法,且每种放法至多放一次 //规定三条边的顺序后 //把所有的block按x递增排序,x相同则按y递增排序 //然后dp // ...

  8. detangle c++ symbols

    hust$ c++filt  _ZN1AC2Ev hust$A::A()

  9. http请求利器: 今天配置出了RESTClient,用MAVEN构建了UI运行包

  10. Swift:函数和闭包

    函数 函数是一个完成独立任务的代码块,Swift中的函数不仅可以像C语言中的函数一样有函数的参数和返回值,而且还支持嵌套,并且有函数参数默认值.可变参数等. //定义一个函数,注意参数和返回值,如果没 ...