POJ3278http://poj.org/problem?id=3278
2024-10-18 21:19:18
题目大意: 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 ;
}
最新文章
- 半吊子学习Swift--天气预报程序-获取天气信息
- 关于Tchar
- OllyDBG 1.10
- mysql SQLyog导入导出csv文件
- Sass:一种CSS预处理器语言
- jsp接收相同复合参数 request.getParameterValues()用法
- Hibernate session flush
- AWK 简明教程
- Android 使用加速度传感器实现摇一摇功能及优化
- 0,null,empty,空,false,isset
- Java Web项目(Extjs)报错一
- JS中全等和相等操作符的区别和比较规则
- 事件Event一
- (转)解决NSMutableAttributedString富文本,不同文字大小水平轴对齐问题(默认底部对齐)
- Spring集成Quarz开发环境搭建
- 《C语言程序设计》指针篇<;一>;
- getsockname()/getpeername()函数第一次被调用得到0.0.0.0结果
- java安全性-引用-分层-解耦
- js中常用的内部函数的使用
- 【加密算法】DES
热门文章
- [Lintcode 3sum]三数之和(python,二分)
- CRC校验码
- chrome下float元素下input选中内容bug
- LA 5009 (三分法求极值) Error Curves
- Task &#39;&#39; not found in root project &#39;***&#39;.
- HDU 1233 还是畅通工程(最小生成树,prim)
- 当ASP.NET MVC模型验证遇上CKEditor
- Java-泛型编程-使用通配符? extends 和 ? super
- Spring各jar包的作用(转载)
- 【转】ACE开发环境搭建