先写自己的笨方法,不好描述但是一看就懂。两个队列循环储存上一个状态延伸出来的子状态。
先++在判断,所以输出L-1。
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=1e6+5;
int d[maxn];
int n,k;
int l=0;
int s=-1;
void bfs()
{
memset(d,-1,sizeof(d));
queue<int> que1,que2;
que1.push(n);
while(s!=k)
{
l++;
if(l%2==1)
{
while(que1.size())
{
int s=que1.front();
que1.pop();
if(s==k) return;
if(s+1<=k && d[s+1]==-1)
{
que2.push(s+1);
d[s+1]=s+1;
}
if(s-1>=0 && d[s-1]==-1)
{
que2.push(s-1);
d[s-1]=s-1;
}
if(s*2<=k+1 && d[s*2]==-1)
{
que2.push(s*2);
d[s*2]=s*2;
}
}
}
else
{
while(que2.size())
{
int s=que2.front();
que2.pop();
if(s==k) return;
if(s+1<=k && d[s+1]==-1)
{
que1.push(s+1);
d[s+1]=s+1;
}
if(s-1>=0 && d[s-1]==-1)
{
que1.push(s-1);
d[s-1]=s-1;
}
if(s*2<=k+1 && d[s*2]==-1)
{
que1.push(s*2);
d[s*2]=s*2;
}
}
}
}
}
int main()
{
cin >> n >> k;
d[n]=n;
d[k]=k;
bfs();
cout << l-1 << endl;
return 0;
}
然后是找了一个比较好理解的他人的代码:http://blog.youkuaiyun.com/freezhanacmore/article/details/8168265
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=100001;
bool vis[maxn];//标记数组
int step[maxn];//记录到了每一位置所走的步数
queue <int> q;//定义队列
int bfs(int n,int k)
{
int head,next;
q.push(n); //开始FJ在n位置,n入队
step[n]=0;
vis[n]=true; //标记已访问
while(!q.empty()) //当队列非空
{
head=q.front(); //取队首
q.pop(); //弹出对首
for(int i=0;i<3;i++) //FJ的三种走法
{
if(i==0) next=head-1;
else if(i==1) next=head+1;
else next=head*2;
if(next<0 || next>=maxn) continue; //排除出界情况
if(!vis[next]) //如果next位置未被访问
{
q.push(next); //入队
step[next]=step[head]+1; //步数+1
vis[next]=true; //标记已访问
}
if(next==k) return step[next]; //当遍历到结果,返回步数
}
}
}
int main()
{
int n,k;
while(cin>>n>>k)
{
memset(step,0,sizeof(step));
memset(vis,false,sizeof(vis));
while(!q.empty()) q.pop(); //注意调用前要先清空
if(n>=k) printf("%d\n",n-k);
else printf("%d\n",bfs(n,k));
}
return 0;
}
与我的不同就是只用了一个while,用一个step数组记录步数相当于我的L,然后每次从队列取元素即可。