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 xsc351072127 at 2013-03-17 09:36:44 on Problem 3278
#include<stdio.h>
int N,K;
int vis[100001]={0};
int line[100001]={0};
int que[201001]={0};

void bfs()
{
    int top=0,end=0;
    que[end++]=N;
    line[N]=0;
    while(top<end)
    {
        int nextm=que[top++];if(nextm==K)break;
        vis[nextm]=1;
        if(2*nextm<=100000&&!vis[2*nextm]&&nextm<K)\\第二种走法
         {
                vis[2*nextm]=1;
                line[2*nextm]=line[nextm]+1;
                que[end++]=2*nextm;
         }
        if(nextm-1>=1&&!vis[nextm-1])\\第一种走法
        {
                vis[nextm-1]=1;
                line[nextm-1]=line[nextm]+1;
                que[end++]=nextm-1;
        }
        if(nextm+1<=100000&&!vis[nextm+1]&&nextm<K)\\第一种走法
        {
                vis[nextm+1]=1;
                line[nextm+1]=line[nextm]+1;
                que[end++]=nextm+1;
        }
    }
}

int main()
{
    while(scanf("%d%d",&N,&K)!=EOF)
    {
       for(int i=0;i<100001;i++)vis[i]=0;
       bfs();
       printf("%d\n",line[K]);
    }
   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