Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
测试数据都过了 怎么就是错得 求大牛#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator