diff --git a/POJ/3278.cpp b/POJ/3278.cpp new file mode 100644 index 0000000..cd3341e --- /dev/null +++ b/POJ/3278.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +using namespace std; + +struct treenode +{ + int value; + treenode *plus,*reduce,*add; +}; + +typedef struct treenode tree; + +tree* root=NULL; + +stack bus; +stack tmpbus; +stack answerbus; + +bool vis[100001]; + +int main() +{ + int n,k; + scanf("%d %d",&n,&k); + root=new (nothrow) tree; + root->value=n; + bus.push(root); + int currentstep=-1; + while(answerbus.empty()) + { + ++currentstep; + while(!bus.empty()) + { + tree* father=bus.top(); + vis[father->value]=true; + if(father->value==k) + { + answerbus.push(father); + break; + } + bus.pop(); + int vplus=father->value*2,vreduce=father->value-1,vadd=father->value+1; + if(vplus<100001&&!vis[vplus]) + { + tree* node=new (nothrow) tree; + node->value=vplus; + father->plus=node; + tmpbus.push(node); + } + if(vadd<100001&&!vis[vadd]) + { + tree* node=new (nothrow) tree; + node->value=vadd; + father->add=node; + tmpbus.push(node); + } + if(vreduce>-1&&!vis[vreduce]) + { + tree* node=new (nothrow) tree; + node->value=vreduce; + father->reduce=node; + tmpbus.push(node); + } + } + while(!tmpbus.empty()) + { + bus.push(tmpbus.top()); + tmpbus.pop(); + } + } + printf("%d\n",currentstep); + return 0; +}