Can think of a wide search to solve this problem is also enough Diao: wide search to the target node can be answered steps
#include <iostream>#include<queue>#include<cstring>using namespacestd;structnode{intValue, Steps; Node (intNints): Value (N), Steps (s) {}};queue<node>Que;intvisited[100000];intMain () {intN, K; Memset (visited,0,sizeof(visited)); CIN>>N>>K; Que.push (Node (N,0)); Visited[n]=1; while( !Que.empty ()) {Node Top=Que.front (); if(Top.value = =K) {cout<<Top.Steps<<Endl; Break; } if(top.value-1>=0&& visited[top.value-1]==0) {Que.push (node (top.value-1, top.steps+1) ); Visited[top.value-1] =1; } if(top.value+1<=100000&&visited[top.value+1]==0) {Que.push (node (top.value+1, top.steps+1) ); Visited[top.value+1] =1; } if(top.value*2<=100000&&visited[top.value*2]==0) {Que.push (node (top.value*2, top.steps+1) ); Visited[top.value*2] =1; } que.pop (); } return 0;}
"Turn" POJ-3278 Catch that Cow:bfs