Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我很无语G++不过C++倒能过

Posted by 154115081034 at 2016-07-27 17:53:01 on Problem 3278
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
#define oo 0x3f3f3f3f
#define N 200100

int vis[N];

struct node
{
    int x, s;
} a, b;

int BFS(int n, int k)
{
    queue<node> q;
    a.x=n;
    a.s=0;
    q.push(a);
    vis[n]=1;

    while(!q.empty())
    {
        a=q.front();
        q.pop();
        if(a.x==k)
            return a.s;
        for(int i=1; i<=3; i++)
        {
            if(i==1) b.x=a.x+1;
            else if(i==2) b.x=a.x-1;
            else b.x=a.x*2;

            if(b.x<0 || b.x>100000)
                continue;

            if(!vis[b.x])
            {
                vis[b.x]=1;
                b.s=a.s+1;
                q.push(b);
            }
        }
    }
}

int main()
{
    int n, k;

    while(~scanf("%d%d", &n, &k))
    {
        memset(vis, 0, sizeof(vis));
        if(n>=k)
            printf("%d\n", n-k);
        else
            printf("%d\n", BFS(n, k));
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator