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

Posted by huzuosheng at 2007-07-26 01:29:00 on Problem 3278
In Reply To:我用BFS 一直是WA请高手指点指点 ?SOS Posted by:keky at 2007-07-20 17:24:42
先申明我是新手刚做题不久 帮你改了下 测了十几组数据测试都可以 但交上去还是被wa
我也没办法了 如果有需要可以看下    


#include<stdio.h>
#include<string.h>

int main()
{
    int N, K, t, n, i;
    int p[20001];
    int q[200001];

	#ifndef ONLINE_JUDGE
    freopen ("3278.txt","r",stdin);
    #endif

    while( scanf("%d %d", &N, &K) == 2 )
    {
		int minnum=999999;

		if(N>=K)
		{
			printf("%d\n",N-K);
			continue;
		}
        memset( q, -1, sizeof( q ) );
        
        q[N] = 0;
        p[0] = N; n = 1; i = 0; t = 0;
        while( i !=n&& p[i]!=K )
        {
            if(q[(p[i]-1)] == -1 && p[i] >=1 ) 
            {
                p[n] = p[i] - 1;
                q[p[n]] = q[p[i]] + 1;
                if( p[n] == K )
                {
                    t = q[p[n]];
					if(t<minnum)   minnum=t;
                }
                n++;
                n%=20001;
            }  
/*			else if(p[i] >=1&&q[p[i]-1]>q[p[i]]+1)
			{
				q[p[i]-1]=q[p[i]]+1;
				
				if( p[i]-1 == K )
                {
                    t = q[p[i]-1];
                    if(t<minnum)   minnum=t;
                }
				
			}*/

				   
            if( ( p[i] + 1 ) < 200001&&q[(p[i] + 1)] == -1    ) 
            {
                p[n] = p[i] + 1;
                q[p[n]] = q[p[i]] + 1;
                if( p[n] == K )
                {
                    t = q[p[n]];
                    if(t<minnum)   minnum=t;
                }
                n++;   
				n%=20001;
            }    
/*			else if(( p[i] + 1 ) < 200001&&q[p[i]+1]>q[p[i]]+1)
			{
				q[p[i]+1]=q[p[i]]+1;
				
				if( p[i]+1 == K )
                {
                    t = q[p[i]+1];
                    if(t<minnum)   minnum=t;
                }
				
			}
				  */

            if( ( 2 * p[i] ) < 200001 && q[(2 * p[i])] == -1  ) 
            {
                p[n] = 2 * p[i];
                q[p[n]] = q[p[i]] + 1;
                if( p[n] == K )
                {    t = q[p[n]];
                    if(t<minnum)   minnum=t;
                }
                n++;  
				n%=20001;
				          
			}             
	/*		else if(( 2 * p[i] ) < 200001 &&q[2 * p[i]]>q[p[i]]+1)
			{
				q[2 * p[i]]=q[p[i]]+1;
				
				if( 2 * p[i] == K )
                {
                    t = q[2 * p[i]];
                    if(t<minnum)   minnum=t;
                }				
			}*/

			i++;
		   	i%=20001;
        }  
		printf("%d\n", minnum);
    }
    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