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

要先判断是否超范围再判断是否访问过。。。弱鸡贴个代码,BFS 47MS

Posted by 1602140212 at 2016-07-27 09:04:30 on Problem 3278
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int maxn = 100005;

int posn,posk;
int t[maxn];

int bfs()
{
    memset(t,-1,sizeof(t));
    queue<int> que;
    que.push(posn);
    t[posn] = 0;


    int temp;
    while(que.size()){
        temp = que.front();
        que.pop();
        if(temp == posk){
            break;
        }

        int next;
        next = temp - 1;
        if(0 <= next && next <= maxn - 5 && t[next] == -1){
            que.push(next);
            t[next] = t[temp] + 1;
        }

        next = temp + 1;
        if(0 <= next && next <= maxn - 5 && t[next] == -1){
            que.push(next);
            t[next] = t[temp] + 1;
        }

        next = temp + temp;
        if(0 <= next && next <= maxn - 5 && t[next] == -1){
            que.push(next);
            t[next] = t[temp] + 1;
        }
    }
    return t[temp];
}

int main()
{
    while(scanf("%d %d",&posn,&posk) != EOF){
        printf("%d\n",bfs());
    }
    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