POJ3278http://poj.org/problem?id=3278

http://poj.org/problem?id=3278

题目大意: 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 <= 100000 && x >= 0 && !vis[x])
        return 1;
    return -1;
}

int BFS(int x)
{
    queue<node>Q;
    node now, next;
    now.x = x;
    now.step = 0;
    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 + 1;
        if(judge(next.x) == 1)
        {
            vis[next.x] = true;
            next.step = now.step + 1;
            Q.push(next);
        }
        next.x = now.x - 1;
        if(judge(next.x) == 1)
        {
            vis[next.x] = true;
            next.step = now.step + 1;
            Q.push(next);
        }
        next.x = now.x * 2;
        if(judge(next.x) == 1)
        {
            vis[next.x] = true;
            next.step = now.step + 1;
            Q.push(next);
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d", &m, &n)!= EOF)
    {
        memset(vis, false, sizeof(vis));
        printf("%d\n", BFS(m));
    }
    return 0;
}
 
时间: 2024-08-07 11:59:08

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

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

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 76935   Accepted: 24323 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00

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 friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shorte

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

http://poj.org/problem?id=1651Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6188   Accepted: 3777 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. Dur

http://poj.org/problem?id=1330

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17818   Accepted: 9455 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each

http://poj.org/problem?id=2112_网络流

1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 using namespace std; 7 8 #define MAX 300 9 #define INF 10000000 10 int dis[MAX][MAX]; // 任意两点间的最短路径 11 int map[MAX][MAX];

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

/**< */#include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> #include <ctype.h> #define N 310 using namespace std; int d[8][2] = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, -1}, {2, 1}

[题解]poj.org Problem#3468

Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. In

poj 2769 Reduced ID Numbers(memset使用技巧)

Description T. Chur teaches various groups of students at university U. Every U-student has a unique Student Identification Number (SIN). A SIN s is an integer in the range 0 ≤ s ≤ MaxSIN with MaxSIN = 106-1. T. Chur finds this range of SINs too larg

POJ 2769 Reduced ID Numbers

思路: 枚举 Reduced ID Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8847 Accepted: 3552 Description T. Chur teaches various groups of students at university U. Every U-student has a unique Student Identification Number (SIN). A SIN s