题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<queue>
#define max(a, b)(a > b ? a : b)
#define N 100010 using namespace std; struct node
{
int x, step;
}; int m, n;
bool vis[N]; int judge(int x)
{
if(x <= && x >= && !vis[x])
return ;
return -;
} int BFS(int x)
{
queue<node>Q;
node now, next;
now.x = x;
now.step = ;
vis[now.x] = true;
Q.push(now);
while(!Q.empty())
{
now = Q.front();
Q.pop();
if(now.x == n)
return now.step;
next.x = now.x + ;
if(judge(next.x) == )
{
vis[next.x] = true;
next.step = now.step + ;
Q.push(next);
}
next.x = now.x - ;
if(judge(next.x) == )
{
vis[next.x] = true;
next.step = now.step + ;
Q.push(next);
}
next.x = now.x * ;
if(judge(next.x) == )
{
vis[next.x] = true;
next.step = now.step + ;
Q.push(next);
}
}
return -;
}
int main()
{
while(scanf("%d%d", &m, &n)!= EOF)
{
memset(vis, false, sizeof(vis));
printf("%d\n", BFS(m));
}
return ;
}

最新文章

  1. 半吊子学习Swift--天气预报程序-获取天气信息
  2. 关于Tchar
  3. OllyDBG 1.10
  4. mysql SQLyog导入导出csv文件
  5. Sass:一种CSS预处理器语言
  6. jsp接收相同复合参数 request.getParameterValues()用法
  7. Hibernate session flush
  8. AWK 简明教程
  9. Android 使用加速度传感器实现摇一摇功能及优化
  10. 0,null,empty,空,false,isset
  11. Java Web项目(Extjs)报错一
  12. JS中全等和相等操作符的区别和比较规则
  13. 事件Event一
  14. (转)解决NSMutableAttributedString富文本,不同文字大小水平轴对齐问题(默认底部对齐)
  15. Spring集成Quarz开发环境搭建
  16. 《C语言程序设计》指针篇&lt;一&gt;
  17. getsockname()/getpeername()函数第一次被调用得到0.0.0.0结果
  18. java安全性-引用-分层-解耦
  19. js中常用的内部函数的使用
  20. 【加密算法】DES

热门文章

  1. [Lintcode 3sum]三数之和(python,二分)
  2. CRC校验码
  3. chrome下float元素下input选中内容bug
  4. LA 5009 (三分法求极值) Error Curves
  5. Task &#39;&#39; not found in root project &#39;***&#39;.
  6. HDU 1233 还是畅通工程(最小生成树,prim)
  7. 当ASP.NET MVC模型验证遇上CKEditor
  8. Java-泛型编程-使用通配符? extends 和 ? super
  9. Spring各jar包的作用(转载)
  10. 【转】ACE开发环境搭建