どこでもドア:http://poj.org/problem?id=3278
AC CODE:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<set>
#include<cstdlib>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int M= 100000+10;
int n,m,ans[M],vis[M];
int main()
{
int n,m;
cin>>n>>m;
int x;
queue<int>que;
que.push(n);
ans[n] = 0;
while(!que.empty())
{
x = que.front();
que.pop();
vis[x] = 1;
if (x==m)
{
cout<<ans[x]<<endl;
break;
}
if (x-1>=0 && vis[x-1]==0)
{
que.push(x-1);
ans[x-1] = ans[x] + 1;
vis[x-1] = 1;
}
if (x+1<100001 && vis[x+1]==0)
{
que.push(x+1);
ans[x+1] = ans[x] + 1;
vis[x+1] = 1;
}
if (x*2<100001 && vis[x*2]==0)//这里判断,&&前后判断颠倒位置(写成vis[x*2]==0&&x*2<100001)就会re,这样就ac。。原因待解。。
{
que.push(x*2);
ans[x*2] = ans[x] + 1;
vis[x*2] = 1;
}
}
return 0;
}