题目大意: 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 ;
}

POJ3278http://poj.org/problem?id=3278的更多相关文章

  1. http://poj.org/problem?id=3278(bfs)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 76935   Accepted: 24323 ...

  2. poj 1651 http://poj.org/problem?id=1651

      http://poj.org/problem?id=1651Multiplication Puzzle   Time Limit: 1000MS   Memory Limit: 65536K To ...

  3. poj-3056 http://poj.org/problem?id=3056

    http://poj.org/problem?id=3056 The Bavarian Beer Party Time Limit: 6000MS   Memory Limit: 65536K Tot ...

  4. poj 1679 http://poj.org/problem?id=1679

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  5. 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258

    #include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...

  6. poj 1915 http://poj.org/problem?id=1915

    /**< */#include <stdio.h> #include <string.h> #include <stdlib.h> #include < ...

  7. Roadblocks http://poj.org/problem?id=3255

    Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best ...

  8. http://poj.org/problem?id=2253

    floyd的应用求每条路径两点之间最大距离的最小值 #include <iostream> #include <cstdio> #include <algorithm&g ...

  9. 线段树 (区间更新,区间查询) poj http://poj.org/problem?id=3468

    题目链接 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...

随机推荐

  1. bzoj2829

    裸题,直接上凸包,然后加上一个圆周即可 只是在这之前没写过旋转而已 const pi=3.14159265358979323; eps=1e-8; type point=record x,y:doub ...

  2. ASP.NET线程相关配置

    1.(maxWorkerThreads * CPU逻辑数量)-minFreeThreads 比如2个CPU默认配置maxWorkerThreads=100,minFreeThreads=176,则同时 ...

  3. bzoj1875: [SDOI2009]HH去散步

    终于A了...早上按自己以前的写法一直WA.下午换了一种写法就A了qwq #include<cstdio> #include<cstring> #include<iost ...

  4. BZOJ2482: [Spoj1557] Can you answer these queries II

    题解: 从没见过这么XXX的线段树啊... T_T 我们考虑离线做,按1-n一个一个插入,并且维护区间[ j,i](i为当前插入的数)j<i的最优值. 但这个最优值!!! 我们要保存历史的最优值 ...

  5. BZOJ2229: [Zjoi2011]最小割

    题解: 真是一道神题!!! 大家还是围观JZP的题解吧(网址找不到了...) 代码: #include<cstdio> #include<cstdlib> #include&l ...

  6. SDOI 2010 and SXOI 2014 地精部落 (递推)

    用E[i,j]表示共有i个数字,以1..j开头且一开始下降的方案数的总和.则我们有: E[i,j]:=E[I,J-1]+E[i-1,i-j] 我们先来证明上升与下降的方案是一一对应的. 事实上,若有a ...

  7. WordPress数据库研究 (转)

    该系列文章将会详细介绍WordPress数据总体的设计思路.详细介绍WordPress10个数据表的设计.并对WordPress系统中涉及的用户信息.分类信息.链接信息.文章信息.文章评论信息.基本设 ...

  8. Linux Shell编程(2): for while

    ; i < ; i++)) do echo "current number is $i" done SERVICES="80 22 25 110 8000 23 2 ...

  9. vc2005编译ffmpeg以及ffplay

    ffmpeg编译过程:1 http://ffmpeg.zeranoe.com/builds/下载官方提供的源码,win32库和dll.2 新建vc2005 console空工程,把ffmpeg.h,f ...

  10. 【Java集合框架】规则集--Set

    集合: Java主要支持三种: 1.规则集(Set) 用于存储一组不重复的元素 2.线性表(List) 用于存储一个由元素构成的有序集合 3.队列(Queue) 同与数据结构中的队列,存储用先进先出的 ...