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

RE了n次,终于过了,贴代码,大家注意使用队列时范围的问题~~~

Posted by yingxiang720 at 2011-03-22 14:05:57 on Problem 3278
#include <iostream>
#include <queue>
using namespace std;

int n,k;
int number[210000];
int visit[210000];

int main()
{
    while(scanf("%d%d",&n,&k) != EOF)
    {
        queue<int>q;
        memset(number,0,sizeof(number));
        memset(visit,0,sizeof(visit));
        q.push(n);
        number[n] ++;
        visit[n] ++;
        while(!q.empty())
        {
            int cf = q.front();
            if(cf == k) break;
            q.pop();
            if(!visit[cf - 1] && cf - 1 <= 100000 && cf - 1 >= 0)
            {
                q.push(cf - 1);
                visit[cf - 1] ++;
                number[cf - 1] += number[cf] + 1;
            }
            if(!visit[cf + 1] && cf + 1 <= 100000 && cf + 1 >= 0)
            {
                q.push(cf + 1);
                visit[cf + 1] ++;
                number[cf + 1] += number[cf] + 1;
            }
            if(!visit[2 * cf] && cf * 2 <= 100000 && cf * 2 >= 0)
            {
                q.push(2 * cf);
                visit[2 * cf] ++;
                number[2 * cf] += number[cf] + 1;
            }
        }
        while(!q.empty())   q.pop();
        printf("%d\n",number[k] - 1);
    }
    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