catch that cow
http://poj.org/problem?id=3278
1 Source Code 2 3 Problem: 3278 User: SDUT_NULL 4 Memory: 1068K Time: 63MS 5 Language: G++ Result: Accepted 6 7 Source Code 8 #include<cstdio> 9 #include<iostream> 10 #include<algorithm> 11 #include<cstring> 12 #include<queue> 13 bool s[200010]; 14 using namespace std; 15 struct node 16 { 17 int num; 18 int count; 19 } st,st1; 20 void bfs(int n,int k) 21 { 22 queue<node>q; 23 if(n==k) {printf("0\n");return;} 24 st.num=n; 25 st.count=0; 26 s[n]=true; 27 q.push(st); 28 while(!q.empty()) 29 { 30 st1=q.front(); 31 q.pop(); 32 if(st1.num==k)break; 33 else 34 { 35 if(st1.num-1>=0&&st1.num-1<=100010&&!s[st1.num-1]) 36 { 37 st.num=st1.num-1; 38 st.count=st1.count+1; 39 s[st1.num-1]=true; 40 q.push(st); 41 } 42 if(st1.num+1>=0&&st1.num+1<=100010&&!s[st1.num+1]) 43 { 44 st.num=st1.num+1; 45 st.count=st1.count+1; 46 s[st1.num+1]=true; 47 q.push(st); 48 } 49 if(st1.num*2>=0&&st1.num*2<=100010&&!s[st1.num*2]) 50 { 51 st.num=st1.num*2; 52 st.count=st1.count+1; 53 s[st1.num*2]=true; 54 q.push(st); 55 } 56 } 57 } 58 printf("%d\n",st1.count); 59 } 60 int main() 61 { 62 int n,k; 63 while(scanf("%d%d",&n,&k)!=EOF) 64 { 65 memset(s,false,sizeof(s)); 66 bfs(n,k); 67 } 68 return 0; 69 }