题目大意: 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. 函数fsp_header_init

    /**********************************************************************//** Initializes the space he ...

  2. LA 3357 (递推 找规律) Pinary

    n位不含前导零不含连续1的数共有fib(n)个,fib(n)为斐波那契数列. 所以可以预处理一下fib的前缀和,查找一下第n个数是k位数,然后再递归计算它是第k位数里的多少位. 举个例子,比如说要找第 ...

  3. bzoj1877: [SDOI2009]晨跑

    挺裸的最小费用最大流... #include<cstdio> #include<queue> #include<cstring> #include<iostr ...

  4. Codeforces Round #269 (Div. 2)

    A 题意:给出6根木棍,如果有4根相同,2根不同,则构成“bear”,如果剩余两个相同,则构成“elephant” 用一个数组分别储存各个数字出现的次数,再判断即可 注意hash[i]==5的时候,也 ...

  5. hdu 4671 Backup Plan(签到题)

    错成那样,还以为是卡时间卡精度的变态题,结果就那么ac了= = 悔死我了 题意就不概述了,只要处理前两列即可.其中第一列顺序直接扫一遍,第二列要先处理较少的那几种.我是接着第一列用 head[] 继续 ...

  6. busybox filesystem httpd php-5.5.31 sqlite3 webserver

    /******************************************************************** * busybox filesystem httpd php ...

  7. 一天一个Java基础——排序

    插入排序 直接插入排序: 当插入第i个数据元素k时,由前i-1个数据元素组成已排序的数据序列,将k与数据序列中各数据元素依次进行比较后,插入到数据序列的适当位置,使得插入后的数据序列仍是排序的. 直接 ...

  8. Myeclipse中相同变量高亮显示

    不小心搞不显示了,解决: windows/MyEclipse-> preferences-> java-> Editor-> Mark Occurences 勾选即可

  9. 【WEB小工具】jQuery函数

    jQuery-API帮助文档:Click here jQuery简介 jQuery是JavaScript框架,jQuery也是JavaScript代码.使用jQuery要比直接使用JavaScript ...

  10. Oracle日常维护脚本

    1.正常停库流程     ps -ef|grep LOCAL=NO|cut -c 9-15|xargs kill -9      shutdown immediate; 2.备份数据库     bac ...