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 Forrest_chen at 2011-09-25 09:14:39 on Problem 3278
#include<stdio.h>
#include<string.h>

int que[200000];
int visit[200000];
int deep[200000];
int N,K;
void solve();

int main(){
	scanf("%d %d",&N,&K);
	
	solve();
	printf("%d\n",deep[K]);
	
	
	return 0;
}

void solve(){
	int rear,front;
	memset(que,0,sizeof(que));
	memset(visit,0,sizeof(visit));
	memset(deep,0,sizeof(deep));
	front=rear=0;
	que[rear++]=N;
	visit[que[front]]=1;
	if(que[front]==K)
		return;
	deep[que[front]]=0;
	while(front<rear){
		
		if(que[front]-1>=0 && visit[que[front]-1]==0){
			if(que[front]-1==K){
				deep[que[front]-1]=deep[que[front]]+1;
				return;
			}
			else{
				visit[que[front]-1]=1;
				que[rear++]=que[front]-1;
				deep[que[front]-1]=deep[que[front]]+1;
			}
		}
		if(visit[que[front]+1]==0){
			if(que[front]+1==K){
				deep[que[front]+1]=deep[que[front]]+1;
				return;
			}
			else{
				visit[que[front]+1]=1;
				que[rear++]=que[front]+1;
				deep[que[front]+1]=deep[que[front]]+1;
			}
		}
		if(que[front]*2<200000&&visit[que[front]*2]==0){
			if(que[front]*2==K){
				deep[que[front]*2]=deep[que[front]]+1;
				return;
			}
			else{
				deep[que[front]*2]=deep[que[front]]+1;
				visit[que[front]*2]=1;
				que[rear++]=que[front]*2;
			}
		}
		front++;
	}
}

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