标签:amp cto using map define for 数学 struct main
一个玄学的广搜边界>=0,<=k+10,这个边界可能可以数学上证明,其他就是简单的广搜,然后输出步数,就可以了
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define N 100010
using namespace std;
struct ddd
{
int x,s;
};
int n,k;
bool used[N*10];
void bfs(int x)
{
queue<ddd> s;
ddd k1;
k1.x=x;
k1.s=0;
s.push(k1);
while(!s.empty())
{
ddd k2,k3;
k2=s.front();
s.pop();
if(k2.x==k)
{
cout<<k2.s<< endl;
exit(0);
}
k3.x=k2.x*2;
k3.s=k2.s+1;
if(k3.x>=0&&k3.x<=N&&!used[k3.x])
{
used[k3.x]=true;
s.push(k3);
}
for(int i=-1;i<=1;i+=2)
{
k3.x=k2.x+i;
k3.s=k2.s+1;
if(k3.x>=0&&k3.x<=N&&!used[k3.x])
{
used[k3.x]=true;
s.push(k3);
}
}
}
}
int main()
{
cin>>n>>k;
if(n==k)
{
cout<<"0"<< endl;
return 0;
}
bfs(n);
return 0;
}
标签:amp cto using map define for 数学 struct main
原文地址:https://www.cnblogs.com/xzx-1228/p/10976476.html