初看这题,不就是搜索找符合条件的数 嘛,easy,来dfs搞一下 ,然后见陷入TLE 。。。妈蛋 。。忘了 这种找满足条件的东西的时候最好用bfs的规律。
好在用刚学会pair搞,,,数组开小。。。没有剪枝 ,,,又陷入RE。。TLE 的循环中。。搞了很久 终于AC 。。我真的太炸了 水题都能卡。。
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int N,K,book[203000];
queue<pair<int,int> >Q;
void bfs()
{
while(!Q.empty())Q.pop();
Q.push(make_pair(N,0));
memset(book,0,sizeof(book));
book[N]=1;
while(!Q.empty())
{
pair<int,int> now,next;
now=Q.front();
Q.pop();
next=now;
for(int i=0;i<3;i++)
{
if(i==0&&next.first<K)//剪枝
next.first=now.first+1;
if(i==1&&next.first>0)//剪枝
next.first=now.first-1;
if(i==2&&next.first<K&&next.first>0)//剪枝
next.first=now.first*2;
next.second=now.second+1;
if(next.first==K)
{
printf("%d\n",next.second);
return;
}
if(!book[next.first])
{
book[next.first]=1;
Q.push(next);
}
}
}
}
int main()
{
while(~scanf("%d %d",&N,&K))
{
if(N<K)
bfs();
if(N==K)
printf("0\n");
if(N>K)
printf("%d\n",N-K);
}
return 0;
}