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 |
我用BFS 一直是WA请高手指点指点 ?SOS我的代码: #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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator