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

求助啊,WA了N 次。。。。各种都比较了。。为啥搜不过去啊

Posted by 20092134 at 2011-07-13 19:03:30 on Problem 3278
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

int visit[1000008],step[1000008];
int BFS(int p,int k){

	queue<int> all;
 int now,next;
 now=p;
 all.push(now);

 step[p]=0;

 while(!all.empty()){
   now=all.front();
   all.pop();

   next=now*2;
  if(next<200003)
  { if(!visit[next])
   {
	   step[next]=step[now]+1;
	   visit[next]=1;
	   all.push(next);
	   if(next==k)
	     {

	  	   return step[next];
	     }
   }
  }



    next=now-1;
   if(next>=0&&next<200003){
   if(!visit[next])
   {
	   step[next]=step[now]+1;
	   visit[next]=1;
	   all.push(next);
	   if(next==k)
	      {
	   	  // cout<<m<<endl;
	   	   return step[next];
	      }
   }
   }


     next=now+1;
      if(next<200003){
     if(!visit[next])
   {
	   step[next]=step[now]+1;
	   visit[next]=1;
	   all.push(next);
	   if(next==k)
	  	      {
	  	   	  // cout<<m<<endl;
	  	   	   return step[next];
	  	      }
   }
      }
 }

 return 0;
}


int main(){
 int len;
 int p,k;

 while(cin>>p>>k){
   memset(visit,0,sizeof(visit));
   memset(step,0,sizeof(step));

     if(p>=k)len=p-k;
    else
	   len=BFS(p,k);
   cout<<len<<endl;
 }
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