注:本人英语很渣,题目大意大多来自百度~=0=
题目大意
农民约翰需要抓住他的牛,他和他的牛在一条直线上(估计是一维生物),约翰在N (0 ≤ N ≤ 100,000)处,他的牛在 K (0 ≤ K ≤ 100,000) ,约翰下次可以从x移动到x+1或者x-1或者2*x的地方,问约翰最少需要多少步才能找到他的牛。
 
用BFS可以解决~ 不过需要剪枝 不然会MLE 
值得一提的一点是步数不要用结构体保存   或许是我剪枝不够好  用结构体保存会MLE
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #include <cstdlib>
  6. #include <cmath>
  7. #include <cctype>
  8. #define N 200010
  9. using namespace std;
  10.  
  11. bool maps[N];//用来判断此点约翰有没有走过
  12. int a, b, step[N];//a, b 为分别为N, K, //step[i]用来保存约翰走到i点时用了几步 (注 : 不要用结构体保存步数, 不然有可能MLE)
  13.  
  14. void BFS()
  15. {
  16. int q1 = a;
  17. maps[a] = true;
  18. queue<int>q;
  19. q.push(q1);
  20.  
  21. while(!q.empty()) {
  22. int q2 = q.front();
  23. q.pop();
  24. if(q2 == b) {//找到之后输出走了几步
  25. printf("%d\n", step[q2]);
  26. return ;
  27. }
  28. //约翰向走 -1 只有此时他的坐标为正数才会走
  29. int x = q2 - ;
  30. if(!maps[x] && q2 > ) {
  31. maps[x] = true;
  32. q1 = x;
  33. step[q1] = step[q2] + ;
  34. q.push(q1);
  35. }
  36. //由于约翰向后走只能 -1 所以只在q2 < b时才向前走
  37. if(q2 < b){
  38. x = q2 + ;
  39. if(!maps[x]) {
  40. maps[x] = true;
  41. q1 = x;
  42. step[q1] = step[q2] + ;
  43. q.push(q1);
  44. }
  45. x = q2 * ;
  46. if(!maps[x]) {
  47. maps[x] = true;
  48. q1 = x;
  49. step[q1] = step[q2] + ;
  50. q.push(q1);
  51. }
  52. }
  53.  
  54. }
  55. }
  56.  
  57. int main()
  58. {
  59. while(~scanf("%d %d", &a, &b)){
  60. memset(maps, , sizeof(maps));
  61. BFS();
  62. }
  63. return ;
  64. }

POJ 3278 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 (附有Runtime Error和Wrong Answer的常见原因)

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

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

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

  5. POJ 3278 Catch That Cow(bfs)

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

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

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

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

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

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

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

  9. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

  10. POJ - 3278 Catch That Cow BFS求线性双向最短路径

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

随机推荐

  1. 介绍MFSideMenu左右滑动控件的使用

    昨天刚写完侧滑菜单的实例,今天在CocoaChina网站上看到一篇非常好的侧滑菜单设计案例文章,分享给大家.http://www.cocoachina.com/macdev/uiue/2013/071 ...

  2. 使用日期控件datePicker,阻止移动端的自动调取键盘的事件

    方法:简单来说就是阻止input的默认事件. 因为datePicker就是用input来封装的,所以直接阻止input的输入事件就ok: 很简单,把input field属性readonly设置为tr ...

  3. 复旦高等代数 I(16级)思考题

    思考题的说明 一.本学期高代I的思考题面向16级的同学,将不定期地进行更新; 二.欢迎16级的同学通过微信或书面方式提供解答图片或纸质文件给我,优秀的解答可以分享给大家: 三.请大家先独立解答思考题, ...

  4. Cheatsheet: 2016 04.01 ~ 04.30

    .NET String format Setting up Ubuntu for .NET Development ASP.​NET Core and Angular2 - Part 1 - Upda ...

  5. 把数据输出到Word (非插件形式)

    项目开发过程中,我们要把数据以各种各样的形式展现给客户.把数据以文档的形式展现给客户相信是一种比较头疼的问题,如果没有好的方法会 使得我的开发繁琐,而且满足不了客户的需求.接下来我会通过两种开发方式介 ...

  6. Linux时间设置及同步

    Linux系统安装时选择的UTC时间是国际标准时间,而中国处于UTC+8时区,因此安装系统时不要选择UTC时区. 还有就是Linux有两个时钟: 1.Bios时钟及硬件时间 2.Kernel时钟及系统 ...

  7. Selenium:元素等待的4种方法

    1.使用Thread.sleep(),这是最笨的方法,但有时候也能用到而且很实用. 2.隐示等待,隐性等待是指当要查找元素,而这个元素没有马上出现时,告诉WebDriver查询Dom一定时间.默认值是 ...

  8. Oracle SQL explain/execution Plan

    From http://blog.csdn.net/wujiandao/article/details/6621073 1. Four ways to get execution plan(anyti ...

  9. PageObject小结

    写之前想把这次的灵感记录下来: 之前看PO模式几次,始终不得法,感觉一片混乱,可就在一天早上,正在照着别人的代码写自己项目时突然脑海中想通了几个问题:1.为什么要封装页面.2.各个模块的作用以及为什么 ...

  10. Gvim常用命令

    这是一篇较全的vim命令.特记录下来,有稍作修改说明.摘http://hi.baidu.com/ui176/item/b00ae7c0eeaba52847d5c0c5 Vim常用命令 跳到指定的行号: ...