标签:
http://acm.hdu.edu.cn/showproblem.php?pid=2717
//水搜。。。
1 #include<stdio.h> 2 #include<math.h> 3 #include<string.h> 4 #include<stdlib.h> 5 #include<iostream> 6 #include<queue> 7 using namespace std; 8 struct stu{ 9 int x; 10 int t; 11 }abc,q; 12 int n,k; 13 int vis[100005]; 14 void bfs() 15 { 16 queue<struct stu>p; 17 p.push(abc); 18 while(!p.empty()) 19 { 20 abc=p.front(); 21 p.pop(); 22 if(abc.x==k) 23 { 24 printf("%d\n",abc.t); 25 return ; 26 } 27 q.x=abc.x-1; 28 if(q.x>=0&&q.x<=100000&&!vis[q.x]) 29 { 30 q.t=abc.t+1; 31 vis[q.x]=1; 32 p.push(q); 33 } 34 q.x=abc.x+1; 35 if(q.x>=0&&q.x<=100000&&!vis[q.x]) 36 { 37 q.t=abc.t+1; 38 vis[q.x]=1; 39 p.push(q); 40 } 41 q.x=abc.x*2; 42 if(q.x>=0&&q.x<=100000&&!vis[q.x]) 43 { 44 q.t=abc.t+1; 45 vis[q.x]=1; 46 p.push(q); 47 } 48 } 49 } 50 51 int main() 52 { 53 //freopen("in.txt","r",stdin); 54 while(~scanf("%d%d",&n,&k)) 55 { 56 memset(vis,0,sizeof(vis)); 57 abc.x=n; 58 abc.t=0; 59 vis[n]=1; 60 bfs(); 61 } 62 return 0; 63 }
标签:
原文地址:http://www.cnblogs.com/xuesen1995/p/4488681.html