http://poj.org/problem?id=3278
三次RE 以为是队列开小了 一直加大队列 忘记是标记数组的事了 改了判断条件 觉得队列不用开那么大 WA。。又开到100W AC..
View Code
1 #include<stdio.h> 2 #include<string.h> 3 struct node 4 { 5 int x,num; 6 }q[1000001]; 7 int f[1000010]; 8 int p,d; 9 int main() 10 { 11 int i,j,n,k,flag = 0; 12 scanf("%d%d", &n,&k); 13 p = 0; 14 d = 1; 15 q[d].num = 0; 16 q[d++].x = n; 17 while(p!=d) 18 { 19 p++; 20 int g = q[p].x; 21 if(g==k) 22 break; 23 if(g-1>=0&&!f[g-1]) 24 { 25 q[d++].x = g-1; 26 f[g-1] = 1; 27 q[d-1].num = q[p].num+1; 28 } 29 if(g<100000&&!f[g+1]) 30 { 31 q[d++].x = g+1; 32 f[g+1] = 1; 33 q[d-1].num = q[p].num+1; 34 } 35 if(2*g<=100000&&!f[2*g]) 36 { 37 q[d++].x = 2*g; 38 f[2*g] = 1; 39 q[d-1].num = q[p].num+1; 40 } 41 } 42 printf("%d\n",q[p].num); 43 }