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 一直是WA请高手指点指点 ?SOS

Posted by keky at 2007-07-20 17:24:42 on Problem 3278
我的代码:
#include<stdio.h>
#include<string.h>
int main()
{
    int N, K, t, n, i;
	int p[200001];
	int q[200001];
	while( scanf("%d %d", &N, &K) == 2 )
	{
		memset( q, 0, sizeof( q ) );
		if(  N < 0 || N > 100000 || K < 0 || K > 100000  )
			return 0;
		q[N] = 1;
	    p[0] = N; n = 1; i = 0; t = 1;
		while( i < n  && p[i] != K)
		{
		    if(q[(p[i]-1)] == 0 && p[i] > 1 ) 
			{
			    p[n] = p[i] - 1;
                q[p[n]] = q[p[i]] + 1;
                if( p[n] == K )
				{
                    t = q[p[n]];
					break;
				}
				n++;				
			}				
			if( q[(p[i] + 1)] == 0 &&  ( p[i] + 1 ) < 200001 ) 
			{
			    p[n] = p[i] + 1;
                q[p[n]] = q[p[i]] + 1;
                if( p[n] == K )
                {
					t = q[p[n]];
                    break;
				}
                n++;				
			}	
			if( q[(2 * p[i])] == 0 &&  ( 2 * p[i] ) < 200001) 
			{
			    p[n] = 2 * p[i];
                q[p[n]] = q[p[i]] + 1;
                if( p[n] == K )
                {    t = q[p[n]];
					break;
				}
                n++;   				
			}	
			i++;
		}			
        printf("%d\n", t-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