トピックへのリンク:https://cn.vjudge.net/problem/POJ-3278
1の#include <iostreamの> 2の#include <CStringの> 3の#include <cstdioを> 4の#include <キュー> 5の#include <アルゴリズム> 6 の#define MEM(A、B)のmemset(A、B、はsizeof(a)参照) ; 7 使用して 名前空間はstdを、 8 の#define INF 0x3f3f3f3f 9のtypedef 長い 長LL。 10 int型 DIR [ 4 ] [ 2 ] = { 0、1、0、 - 1、1、0、 - 1、0 }。 11 のconst int型 MAXN = 100005 。 12 int型N、K、VIS [MAXN]、ANS。 13 構造体ノード{ 14 int型のLOC、ヴァル。 15 ノード(int型 L、INT V):LOC(L)、ヴァル(V){}。 16 }。 17の ボイド BFS(int型 M){ // (Z、X、Y) 18 VIS [M] = 1 。 19 キュー<ノード> Q。 20 q.push(ノード(M、0 )); 21 ながら(!q.empty()){ 22 ノードTEMP = q.front()。 23 q.pop()。 24 であれば(temp.loc == K){ 25 ANS = temp.val。 26 ブレーク; 27 } 28 int型 FX = temp.loc - 1 。 29 であれば(FX> = 0 && FX <= 100000 &&!VIS [FX]) 30 { 31 VIS [FX] = 1 。 32 q.push(ノード(FX、temp.val + 1 ))。 33 } 34 FX = temp.loc + 1 。 35 であれば(FX> = 0 && FX <= 100000 &&!VIS [FX]){ 36 VIS [FX] = 1 。 37 q.push(ノード(FX、temp.val + 1 ))。 38 } 39 FX = temp.loc * 2 。 40 であれば(FX> = 0 && FX <= 100000 &&!VIS [FX]){ 41 VIS [FX] = 1 。 42 q.push(ノード(FX、temp.val + 1 ))。 43 } 44 } 45 } 46 のint main()の 47 { 48 CIN >> N >> K。 49の BFS(N) 50 COUT << ANS << ENDL。 51 リターン 0 。 52 }