题目传送门

 1 /*
 2     BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手
 3 */
 4 #include <cstdio>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <map>
 8 #include <queue>
 9 #include <set>
10 #include <cmath>
11 #include <cstring>
12 using namespace std;
13
14 const int MAXN = 1e6 + 10;
15 const int INF = 0x3f3f3f3f;
16 int n, m;
17 int d[MAXN];
18 bool vis[MAXN];
19
20 void BFS(void)
21 {
22     memset (vis, 0, sizeof (vis));
23     for (int i=0; i<=1e6; ++i)  d[MAXN] = 0;
24
25     queue<int> q;
26     q.push (n); d[n] = 0;   vis[n] = true;
27
28     while (!q.empty ())
29     {
30         int x = q.front (); q.pop ();
31         if (x == m) break;
32
33         int xl = x - 1; int xr = x + 1; int x2 = x * 2;
34
35         if (xl >= 0 && !vis[xl])
36         {
37             q.push (xl);    d[xl] = d[x] + 1;
38             vis[xl] = true;
39         }
40         if (xr <= 1e6 && !vis[xr])
41         {
42             q.push (xr);    d[xr] = d[x] + 1;
43             vis[xr] = true;
44         }
45         if (x2 <= 1e6 && !vis[x2])
46         {
47             q.push (x2);    d[x2] = d[x] + 1;
48             vis[x2] = true;
49         }
50     }
51
52 }
53
54 int main(void)      //POJ 3278 Catch That Cow
55 {
56     //freopen ("POJ_3278.in", "r", stdin);
57
58     while (scanf ("%d%d", &n, &m) == 2)
59     {
60         BFS ();
61         printf ("%d\n", d[m]);
62     }
63
64     return 0;
65 }

转载于:https://www.cnblogs.com/Running-Time/p/4444809.html

BFS POJ 3278 Catch That Cow相关推荐

  1. POJ 3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 30924   Accepted: 9536 D ...

  2. POJ 3278 Catch That Cow BFS

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32071   Accepted: 9866 D ...

  3. POJ 3278 Catch That Cow(BFS)

    题目网址:http://poj.org/problem?id=3278 题目: Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Tot ...

  4. bfs+dfs分析----poj 3278 Catch That Cow

    题目详情 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 115430   Accepted:  ...

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

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

  6. POJ 3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35043   Accepted: 10800 ...

  7. poj 3278 Catch That Cow(广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45087   Accepted: 14116 ...

  8. poj 3278 Catch That Cow 广搜

    hdu 2717 Catch That Cow,题目链接 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...

  9. poj 3278 Catch That Cow (bfs)

    题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 1 #include< ...

最新文章

  1. 满足极高读写性能需求的Key-Value数据库
  2. zabbix告警升级的迷惑
  3. 基于角色的权限设计(二)
  4. android java和c混合编程_C/C++在Java、Android和Objective-C三大平台下实现混合编程
  5. 算法学习总结(三):插入排序
  6. 不是外部命令也不是可运行程序_手机运行内存4G和6G有什么不同?差别不是一般大...
  7. linux下source filename,./filename,. filename,......
  8. xilinx FIFO的使用及各信号的讨论
  9. 推荐几个e书下载地址
  10. 《计算机操作系统》(第四版)知识点总结
  11. 如何成为一名研发主管--关于个人、过程、工具和团队之一
  12. Imagination发布PowerVR软件开发套件和工具包重要更新版本,含光线追踪代码示例...
  13. 刚才玩了下Steganos Internet Anonym,IP伪装器,每一秒自动换一IP
  14. Python订票系统这才是看电影选座的正确方法,原来我们都上当了
  15. minio Non-XML response from server
  16. SIP协议标准和实现机制
  17. icloud android,果粉当腻了?你和安卓手机只差一个iCloud同步
  18. Relying upon circular references is discouraged and they are prohibited by default.
  19. html 布局-相对位置及绝对位置
  20. 北京至桂林amp;amp;阳朔两宅人自助游,有图有

热门文章

  1. eclipse启动tomcat报错:org.apache.logging.log4j.web.Log4jServletContextListener
  2. ThreadLocalConnection
  3. 数据挖掘概念与技术_文本挖掘
  4. 笔记(2)-文本挖掘与机器学习
  5. Linux编辑启动停止重启springboot jar包脚本
  6. sql server 语法 MSDN
  7. Spring之JDBC模板jdbcTemplate
  8. 支付宝接口调试经验总结
  9. Windows Server 2008 R2之二从介质安装 AD DS
  10. 主流搜索引擎Lucene,Solr,ElasticSearch