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

哪个大佬帮我看看怎么就错了 枯了

Posted by lant at 2020-02-26 12:28:52 on Problem 3278
#include <iostream>
#include <string>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const ll maxn = 100000;

ll N,M;
bool vis[maxn];

struct node 
{
    ll x;
    ll step;
};

void bfs(int n,int k)
{
    node v,t,p;
    queue<node> q;
    p.x = n; p.step = k;
    q.push(p);
    while(!q.empty())
    {
        t = q.front();
        q.pop();
        if(t.x == M)
        {
            cout<<t.step<<endl;
            return;
        }
        if(t.x - 1 >= 0 && !vis[t.x-1])
        {
            v.x = t.x-1; v.step = t.step+1;
            q.push(v);
            vis[v.x] = 1;
        }
        if(t.x + 1 <= maxn && !vis[t.x+1])
        {
            v.x = t.x+1; v.step = t.step+1;
            q.push(v);
            vis[v.x] = 1;
        }
        if(t.x * 2 <= maxn && !vis[t.x*2])
        {
            v.x = t.x*2; v.step = t.step+1;
            q.push(v);
            vis[v.x] = 1;
        }
    }
}

int main()
{
    cin >> N >> M;
    memset(vis,0,sizeof(vis));
    vis[N] = 1;
    bfs(N,0);
    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