标签:poj turn src code image 进一步 scanf memset ios
code
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
queue <int> q;
int vis[100005];
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
while(!q.empty())
{
q.pop();
}
memset(vis,0,sizeof(vis));
q.push(n);
vis[n]=1;///开始搜索时的第一步,之后在 vis[tem-1]=vis[tem]+1;
///vis[tem+1]=vis[tem]+1; vis[tem*2]=vis[tem]+1; 这三种情况即为第2步,以此类推第3.4.5...n 直到找到为止
while(!q.empty())
{
int tem=q.front();
q.pop();
if(tem-1>=0&&!vis[tem-1])
{
q.push(tem-1);
vis[tem-1]=vis[tem]+1;
}///后退一步
if(tem+1<=100000&&!vis[tem+1])
{
q.push(tem+1);
vis[tem+1]=vis[tem]+1;
}///前进一步
if(tem*2<=100000&&!vis[tem*2])
{
q.push(tem*2);
vis[tem*2]=vis[tem]+1;
}///跳跃
if(vis[k])
{
break;
}///到达终点
}
printf("%d\n",vis[k]-1);
}
return 0;
}
标签:poj turn src code image 进一步 scanf memset ios
原文地址:http://www.cnblogs.com/hhkobeww/p/6638991.html