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 1516050107 at 2016-10-04 20:13:43 on Problem 3278
ac代码的:
#include<iostream>
#include<queue>
using namespace std;
typedef long long LL;
const int M= 100000+10;
int ans[M],vis[M];
int main()
{
    int n,m;
    cin>>n>>m;
    int x;
    queue<int>que;
    que.push(n);
    ans[n] = 0;
        while(!que.empty())
        {
            x = que.front();
            que.pop();
            vis[x] = 1;
            if (x==m)
            {
                cout<<ans[x]<<endl;
                break;
            }
            if (x-1>=0 && vis[x-1]==0)
            {
                que.push(x-1);
                ans[x-1] = ans[x] + 1;
                vis[x-1] = 1;
            }
            if (x+1<100001 && vis[x+1]==0)
            {
                que.push(x+1);
                ans[x+1] = ans[x] + 1;
                vis[x+1] = 1;
            }
            if (x*2<100001 && vis[x*2]==0)
            {
                que.push(x*2);
                ans[x*2] = ans[x] + 1;
                vis[x*2] = 1;
            }
        }
    return 0;
}

Re的代码:
int main()
{
    int n,m;
    cin>>n>>m;
    int x;
    queue<int>que;
    que.push(n);
    ans[n] = 0;
        while(!que.empty())
        {
            x = que.front();
            que.pop();
            vis[x] = 1;
            if (x==m)
            {
                cout<<ans[x]<<endl;
                break;
            }
            if (vis[x-1]==0&&x-1>=0)
            {
                que.push(x-1);
                ans[x-1] = ans[x] + 1;
                vis[x-1] = 1;
            }
            if (vis[x+1]==0&&x+1<100001 )
            {
                que.push(x+1);
                ans[x+1] = ans[x] + 1;
                vis[x+1] = 1;
            }
            if (vis[x*2]==0&&x*2<100001 )
            {
                que.push(x*2);
                ans[x*2] = ans[x] + 1;
                vis[x*2] = 1;
            }
        }
    return 0;
}


就是把这几个 if (vis[x*2]==0&&x*2<100001 )里面的&&两边换下顺序就对了。。。

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