首页 > 其他 > 详细

HDU 2717 深搜第一题、

时间:2016-02-01 13:56:25      阅读:234      评论:0      收藏:0      [点我收藏+]

题意:求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 } 

 

HDU 2717 深搜第一题、

原文:http://www.cnblogs.com/sasuke-/p/5174795.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!