bfs,RE多次之后学习大牛的代码,不要惊讶怎么限定在100000范围内!
更好的解法:http://www.cnblogs.com/longzhiri/articles/1555344.html
1 # include <stdio.h>
2 # include <string.h>
3 # include <queue>
4
5 using namespace std;
6
7 int n, k, x;
8 int d[100002];
9 queue<int>Q;
10
11 int main()
12 {
13 while (~scanf("%d%d", &n, &k))
14 {
15 memset(d, 0, sizeof(d));
16 while (!Q.empty()) Q.pop();
17 Q.push(n);
18 while (!Q.empty())
19 {
20 x = Q.front();
21 Q.pop();
22 if (x == k) break;
23 if (x > 0 && 0==d[x-1]) {Q.push(x-1); d[x-1] = d[x]+1;}
24 if (x < 100001 && 0==d[x+1]) {Q.push(x+1); d[x+1] = d[x]+1;}
25 if ((x<<1) < 100001 && 0==d[x<<1]) {Q.push(x<<1); d[x<<1] = d[x]+1;}
26 }
27 printf("%d\n", d[k]);
28 }
29
30 return 0;
31 }