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

判断是否越界时为什么都是 <= 100000,不能先走到10万以后再一下一下的减?题目没说啊

Posted by liuyu523115237 at 2011-04-10 16:02:33 on Problem 3278 and last updated at 2011-04-10 16:08:53
比如:5 0001   10 0000 结果肯定不一样
**************************************************
#include <cstdlib>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=200009;
bool used[maxn];
/*
 * 
 */
bool check(int x)          //都是小于10w?题目没说啊
{
    if(x>=0&&x<maxn)
        return true;
    return false;
}
struct node
{
    int step,idx;
}st,end;
int bfs()
{
    memset(used,false,sizeof(used));
    used[st.idx]=true;
    st.step=0;
    queue<node> que;
    que.push(st);
    while(!que.empty())
    {
        node u=que.front();
        que.pop();
        if(u.idx == end.idx)
            return u.step;
        node v=u;
        v.step++;

        v.idx=u.idx-1;
        if(check(v.idx)&&!used[v.idx])
        {
            used[v.idx]=true;
            que.push(v);
        }
        if(u.idx>=maxn)
            continue;
        v.idx=u.idx+1;
        if(check(v.idx)&&!used[v.idx])
        {
            used[v.idx]=true;
            que.push(v);
        }
        v.idx=u.idx*2;
        if(check(v.idx)&&!used[v.idx])
        {
            used[v.idx]=true;
            que.push(v);
        }
    }
    return -1;
}
int main(int argc, char** argv)
{
    while(cin>>st.idx>>end.idx)
    {
        cout<<bfs()<<endl;
    }
    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