看懂意思就很简单了,给你一个数,每次只能加一或者减一或者乘2,问最少几次才能变成另一个数,就是一个简单的bfs过程,看代码吧。


AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int MAXN = 100005;
struct Node{int x,step;
}Now,Next,S,E;
bool vis[MAXN];                         // 用于标记这个数是否出现过int bfs(){queue<Node> q;memset(vis,0,sizeof(vis));S.step = 0;q.push(S);while(!q.empty()){Now = q.front();q.pop();if(Now.x == E.x){                   // 结束的时候返回步数return Now.step;}for(int i=0;i<3;i++){               // 模拟变换过程if(i==0){Next.x = Now.x + 1;}else if(i==1){Next.x = Now.x - 1;}else{Next.x = Now.x * 2;}if(Next.x >= 0 && Next.x <= 100000 && vis[Next.x] == 0){vis[Next.x] = 1;Next.step = Now.step + 1;q.push(Next);}}}return -1;
}int main()
{while(~scanf("%d%d",&S.x,&E.x)){int temp = bfs();printf("%d\n",temp);}return 0;
}
/***[来源] POJ 3278[题目] Catch That Cow[大意]给你两个数n,m,n每次只能加一或者减一或者乘以2,问最少需要多少次才能变成m[输入]5 17[输出]4
*/



POJ 3287 Catch That Cow(bfs)相关推荐

  1. POJ 3278 Catch That Cow BFS

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

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

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

  3. BFS POJ 3278 Catch That Cow

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

  4. POJ 3278 Catch That Cow

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

  5. POJ 3278 Catch That Cow(BFS)

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

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

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

  7. POJ 3278 Catch That Cow

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

  8. HDU2717 Catch That Cow ( BFS )

    Catch That Cow Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Desc ...

  9. catch that cow(bfs)

    catch that cow 题目描述 花花经过长时间的研究,终于研发出了能够跃迁的宇宙飞船.现在,他想要前往致远星.假设地球和致远星都在一个坐标轴上,其中地球位于坐标n,而致远星位于坐标k.而花花的 ...

最新文章

  1. C语言-数据数据类型、变量与常量
  2. 直接定址表---汇编学习笔记
  3. Ubuntu系统如何安装软件
  4. 【数据结构与算法】之深入解析“按要求补齐数组”的求解思路与算法示例
  5. Django中间件2
  6. 安卓百度地图附近poi搜索以及到指定poi的换乘方案
  7. CSS3 Transform、Transition和Animation属性总结
  8. IDEA——修改idea64.exe.vmoptions文件解决coding卡顿问题
  9. 容器编排技术 -- Kubernetes Replication Controller
  10. solid测序列原理_SOLID原理简介
  11. 分布式系统中的领导选举
  12. java智能提示_【Java】智能提示的设置
  13. Mybatis的两种分页方式:RowBounds和PageHelper
  14. 使用CrossOver安装第三方软件
  15. 关于Polar SI9000重装/启动后卡在Frequency Dependent Calculation界面不能切换。
  16. Android 程序清单详解
  17. 【html】表格table与表单form
  18. php表格中的caption,HTML中的caption属性是什么意思?caption标签在HTML中的用法(附实例)...
  19. 在WIN10中配置conda
  20. 质量团队的组建与升级

热门文章

  1. 让整个页面旋转180度
  2. 如何让Win10锁屏1分钟后不再关闭显示器
  3. 底盘线控悬架智能化趋势
  4. maven打包jar到本地仓库
  5. 【个人笔记】关于string头文件
  6. asp.net高校心理辅导站系统
  7. 初学者笔记——微信小程序点击图片放大
  8. Python绘制loss曲线、准确率曲线
  9. 多项式函数曲线拟合——最小二乘法
  10. PHP通用函数:判断字符串是否是手机号码