poj 3278
题目http://poj.org/problem?id=3278
基础广搜,打表就过了,最坏的情况就100000到1,一开始没注意题目有0,wa了,又一次因为没注意题目带来的血的教训
1 #include <stdio.h> 2 #include<string.h> 3 #include<queue> 4 using namespace std; 5 int mark[900000]; 6 int bfs(int n,int k) 7 { 8 memset(mark,0,sizeof(mark)); 9 queue<int> q; 10 queue<int> s; 11 q.push(n); 12 s.push(0); 13 mark[n]=1; 14 while(!q.empty()) 15 { 16 int n1=q.front(); 17 q.pop(); 18 int steps=s.front(); 19 s.pop(); 20 if(n1==k) 21 return steps; 22 23 24 25 if(n1-1>=0&&mark[n1-1]==0) 26 { 27 q.push(n1-1); 28 s.push(steps+1); 29 mark[n1-1]=1; 30 } 31 if(n1+1<=k&&mark[n1+1]==0) 32 { 33 q.push(n1+1); 34 s.push(steps+1); 35 mark[n1+1]=1; 36 } 37 if(n1*2<=3*k&&mark[n1*2]==0) 38 { 39 q.push(n1*2); 40 s.push(steps+1); 41 mark[n1*2]=1; 42 } 43 } 44 return 0; 45 } 46 47 48 int main(int argc, char *argv[]) 49 { 50 int n,k; 51 while (scanf("%d %d",&n,&k)!=EOF) 52 { 53 printf("%d\n",bfs(n,k)); 54 } 55 return 0; 56 }