Topic links: https://vjudge.net/problem/POJ-3278
Meaning of the questions: one can move one space left and right to move one space, or move to the current location at twice the target grid
Ideas: the three cases that Italy ran bfs, the first to reach the destination in the shortest time.
1 #include <iostream>
2 #include <string.h>
3 #include<queue>
4 #include <algorithm>
5 using namespace std;
6
7 #define rep(i,j,k) for(int i = (j); i <= (k); i++)
8 #define per(i,j,k) for(int i = (j); i >= (k); i--)
9
10 const int N = 1e5 + 10;
11 int mv[] = { -1, 1, 2 };//2表示跳跃
12 bool vis[N];
13 int n-, K;
14
15 struct Node {
16 int X, C;
. 17 };
18 is
. 19 // determines whether or not within the limits of
20 is inline BOOL Check ( int X) {
21 is return X> = 0 && X <= 100000 ;
22 is }
23 is
24 void BFS () {
25
26 is VIS [n-] = to true ;
27 Queue <Node> que;
28 que.push ({n-Node, 0 });
29
30 //Direct catch
31 is IF (n-== K) {
32 COUT << 0 << endl;
33 is return ;
34 is }
35
36 int DX;
37 [ the while (! {Que.empty ())
38 is Node tmp = que.front ( );
39 que.pop ();
40
41 is REP (I, 0 , 2 ) {
42 is IF (I == 2 ) DX = tmp.x << . 1 ; // jump tmp.c * 2 = DX
43 is the elsetmp.x + = DX Music Videos [I];
44 is
45 // if not been accessed and within the confines
46 is IF (Check (DX) &&! VIS [DX]) {
47 VIS [DX] = to true ; // tag access after
48
49 @ caught
50 IF (DX == K) {
51 is COUT << tmp.c + . 1 << endl;
52 is return ;
53 is }
54 is the else que.push (Node {DX, tmp.c + . 1 });
55 }
56 }
57 }
58
59 }
60
61 int main(){
62
63 ios::sync_with_stdio(false);
64 cin.tie(0);
65
66 cin >> n >> k;
67 bfs();
68
69 return 0;
70 }