HDU 2717 宽搜第一题、

题意:求n到k的最小路径,  n有三种变法 n+1,n-1或者2*n;

贴个广搜的模版在这里把....

总结一下:一般涉及到求最短路的话用宽搜

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<queue>
 5 #include<cstring>
 6 using namespace std;
 7 const int qq=1e5+10;
 8 int vis[qq];
 9 int n,k;
10 struct num{
11     int x,step;
12 };
13 int check(int x)
14 {
15     if(x<0||x>qq||vis[x])
16         return 0;
17     return 1;
18 }
19 int bfs(int x)
20 {
21     int i;
22     queue<num>Q;
23     num a,next;
24     a.x=x;
25     a.step=0;
26     vis[x]=1;
27     Q.push(a);
28     while(!Q.empty()){
29         a=Q.front();
30         Q.pop();
31         if(a.x==k)
32             return a.step;
33         next=a;                //将三种情况加入队列、 
34         next.x=a.x+1;
35         if(check(next.x)){
36             next.step=a.step+1;
37             vis[next.x]=1;
38             Q.push(next);
39         }
40         next.x=a.x-1;
41         if(check(next.x)){
42             next.step=a.step+1;
43             vis[next.x]=1;
44             Q.push(next);
45         }
46         next.x=a.x*2;
47         if(check(next.x)){
48             next.step=a.step+1;
49             vis[next.x]=1;
50             Q.push(next);
51         }
52     }
53     return -1;
54 }
55 int main()
56 {
57     while(cin >> n >> k){
58         memset(vis,0,sizeof(vis));
59         int ans=bfs(n);
60         cout << ans << endl;
61     }
62 } 
原文地址:https://www.cnblogs.com/sasuke-/p/5174795.html