bfs的思想
用了两个队列,队列m读入数,n读入状态,对应关系。相当于分割符,0101输入,当与上次出队列的数字不同时,说明到了下一层。counnt计数加1.
其实用结构体更好。
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int main()
{
int a,b;
scanf("%d%d",&a,&b);
queue<int >m;
queue<int >n;
m.push(a);
n.push(1);
int y=0;
int counnt=0;
int c=a;
while(1)
{
int c=m.front();
m.pop();
int x=n.front();
if(x!=y)
{
counnt++;
y=n.front();
}
n.pop();
if((c+1)<=100000&&(c+1)>=0)
{
m.push(c+1);
if(x==1) n.push(0);
else
{
n.push(1);
}
if(c+1==b)
break;
}
if((c-1)<=100000&&(c-1)>=0)
{
m.push(c-1);
if(x==1) n.push(0);
else
{
n.push(1);
}
if(c-1==b)
break;
}
if((c*2)<=100000&&(c*2)>=0)
{
m.push(c*2);
if(x==1) n.push(0);
else
{
n.push(1);
}
if(c*2==b)
break;
}
}
cout<<counnt<<endl;
system("pause");
return 0;
}