Description

  Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

  * Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
  *Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

  If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

  也是典型的BFS的题目,我第一次设的数组最大是100000*10,然后过了,第二次100000+5,居然也过了。。。

  这个题就是从出发点开始bfs,有三条路可以走,一知道发现牛为止。

代码如下:

  1. #include<iostream>
  2. #include<cstring>
  3. #include<queue>
  4.  
  5. using namespace std;
  6.  
  7. int N,K;
  8. int rem[];
  9.  
  10. void bfs()
  11. {
  12. queue <int> que;
  13. int t;
  14.  
  15. que.push(N);
  16. rem[N]=;
  17.  
  18. while(!que.empty())
  19. {
  20. t=que.front();
  21. que.pop();
  22.  
  23. if(t==K)
  24. return;
  25.  
  26. if(t*<=&&rem[t*]==-)
  27. {
  28. rem[t*]=rem[t]+;
  29. que.push(t*);
  30. }
  31. if(t+<=&&rem[t+]==-)
  32. {
  33. rem[t+]=rem[t]+;
  34. que.push(t+);
  35. }
  36. if(t->=&&rem[t-]==-)
  37. {
  38. rem[t-]=rem[t]+;
  39. que.push(t-);
  40. }
  41. }
  42. }
  43.  
  44. int main()
  45. {
  46. ios::sync_with_stdio(false);
  47.  
  48. while(cin>>N>>K)
  49. {
  50. memset(rem,-,sizeof(rem));
  51.  
  52. if(K<=N)
  53. cout<<N-K<<endl;
  54. else
  55. {
  56. bfs();
  57. cout<<rem[K]<<endl;
  58. }
  59. }
  60.  
  61. return ;
  62. }

(简单) POJ 1278 Catch That Cow,回溯。的更多相关文章

  1. BFS POJ 3278 Catch That Cow

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

  2. POJ 3278 Catch That Cow(赶牛行动)

    POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Farmer J ...

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

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

  4. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

  5. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  6. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  7. POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  8. POJ 3278 Catch That Cow(求助大佬)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 109702   Accepted: 34255 ...

  9. POJ 3278 Catch That Cow(bfs)

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

随机推荐

  1. Sql Server异常捕获 try catch

    declare @num int begin try end try begin catch select ERROR_LINE() as 错误行数, ERROR_MESSAGE() as 错误消息 ...

  2. subversion-fundamental concepts

    1.在使用svn 的时候,版本号version的问题一直困恼. 如在目录/app/controller上面右键选择查看该目录的 show log ,弹出的窗口显示的Revision log.单击每一条 ...

  3. linux cat more less head tail

    cat 命令: cat filename  查看一个文件的内容cat[选项][文件]... -b  对非空白行进行编号,行号从1开始-n  和nl命令差不多,对所有行(包括空白行)进行编号输出显示-E ...

  4. js框架——angular.js(2)

    1. 模块的利用扩充 模块的名称也可以当做变量使用,例如: <body ng-app> <label><input type="checkbox" n ...

  5. [LeetCode] Magical String 神奇字符串

    A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...

  6. Windows API 之 CreateThread、WaitForSingleObject(未完)

    WaitForSingleObject Waits until the specified object is in the signaled state or the time-out interv ...

  7. hdu_5589_Tree(莫队+字典树)

    题目连接:hdu_5589_Tree 题意:给你一棵树和一些边值,n个点n-1条边,一个m,q个询问,每个询问让你输出在[l,r]区间内任意两点树上的路径的边权异或的和大于m的点对数. 题解:这题很巧 ...

  8. C#排列组合类

    //----------------------------------------------------------------------------- // // 算法:排列组合类 // // ...

  9. 基于ATmgea8单片机设计的加热控制系统(转)

    源:http://blog.163.com/zhaojun_xf/blog/static/3005058020085102562729/ 1 引言 温度是工业生产中主要的被控参数之一,与之相关的各种温 ...

  10. idx_rebuild_diff_idx_l.sql

    1. set echo on feedback on set timing on set time on set pagesize 1000 set linesize 150 spool 02_do_ ...