题目通道
解法
bfs+queue
写法一
//Accepted 736K 0MS C++ 1017B 2012-11-10 04:24:09
//#include"pch.h"
//#pragma warning(disable:4996);
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std;
//#define _CRT_SECURE_NO_DEPRECATE;
//#define _CRT_SECURE_NO_WARNINGS;
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define LL long long
#define eps 0.000001
#define inf 0x3f3f3f3f
#define exp 0.000001
#define pai 3.141592654
#define random(x) rand()%(x)
#define lowbit(x) x&(-x)
inline int read()
{
int x = 0, y = 1; char a = getchar(); while (a > '9' || a < '0') { if (a == '-')y = -1; a = getchar(); }
while (a >= '0' && a <= '9') { x = 10 * x + a - '0'; a = getchar(); }return x * y;
}
const int maxn=100001;
bool vis[maxn];
int n,k;
struct node
{
int x,step;
};
queue<node>q;
int bfs()
{
int i;
node now,next;
now.x = n; now.step = 0;
q.push(now);
vis[n]=true;
while( q.empty() == 0 )
{
now = q.front();
q.pop();
if ( now.x == k ){
return now.step;
}
for(i=0;i<3;i++)
{
if(i==0) next.x=now.x-1;
else if(i==1) next.x=now.x+1;
else next.x=2*now.x;
if(next.x<0 || next.x>=maxn) continue;//剪枝、排除越界
if( vis[next.x] ) continue;
vis[next.x]=true;
next.step=now.step+1;
q.push(next);
// if(next.x==k) return next.step;
}
}
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(vis,0,sizeof(vis));
printf("%d\n",bfs());
}
return 0;
}
写法二
//Accepted 736K 0MS C++ 1017B 2012-11-10 04:24:09
//#include"pch.h"
//#pragma warning(disable:4996);
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std;
//#define _CRT_SECURE_NO_DEPRECATE;
//#define _CRT_SECURE_NO_WARNINGS;
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define LL long long
#define eps 0.000001
#define inf 0x3f3f3f3f
#define exp 0.000001
#define pai 3.141592654
#define random(x) rand()%(x)
#define lowbit(x) x&(-x)
inline int read()
{
int x = 0, y = 1; char a = getchar(); while (a > '9' || a < '0') { if (a == '-')y = -1; a = getchar(); }
while (a >= '0' && a <= '9') { x = 10 * x + a - '0'; a = getchar(); }return x * y;
}
const int maxn=100001;
bool vis[maxn];
int n,k;
struct node
{
int x,step;
};
queue<node>q;
int bfs()
{
node now,next;
now.x = n; now.step = 0;
q.push(now);
vis[n]=true;
while( q.empty() == 0 )
{
now = q.front();
q.pop();
for(int i=0;i<3;i++)
{
if(i==0) next.x=now.x-1;
else if(i==1) next.x=now.x+1;
else next.x=2*now.x;
if(next.x<0 || next.x>=maxn) continue;//剪枝、排除越界
if( vis[next.x] ) continue;
vis[next.x]=true;
next.step=now.step+1;
q.push(next);
if(next.x==k) return next.step;
}
}
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(vis,0,sizeof(vis));
printf("%d\n",bfs());
}
return 0;
}
写法三
//Accepted 736K 0MS C++ 1017B 2012-11-10 04:24:09
//#include"pch.h"
//#pragma warning(disable:4996);
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std;
//#define _CRT_SECURE_NO_DEPRECATE;
//#define _CRT_SECURE_NO_WARNINGS;
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define LL long long
#define eps 0.000001
#define inf 0x3f3f3f3f
#define exp 0.000001
#define pai 3.141592654
#define random(x) rand()%(x)
#define lowbit(x) x&(-x)
inline int read()
{
int x = 0, y = 1; char a = getchar(); while (a > '9' || a < '0') { if (a == '-')y = -1; a = getchar(); }
while (a >= '0' && a <= '9') { x = 10 * x + a - '0'; a = getchar(); }return x * y;
}
const int maxn=100001;
bool vis[maxn];
int n,k;
struct node
{
int x,step;
};
queue<node>q;
int bfs()
{
int i;
node now,next;
now.x = n; now.step = 0;
q.push(now);
vis[n]=true;
while( q.empty() == 0 )
{
now = q.front();
q.pop();
if ( now.x == k ){
return now.step;
}
next.step = now.step+1;
next.x = now.x * 2;
if ( next.x >= 0 && next.x < maxn && vis[next.x]==0){
vis[next.x]=true;
q.push(next);
}
next.step = now.step+1;
next.x = now.x + 1;
if ( next.x >= 0 && next.x < maxn && vis[next.x]==0){
vis[next.x]=true;
q.push(next);
}
next.step = now.step+1;
next.x = now.x - 1;
if ( next.x >= 0 && next.x < maxn && vis[next.x]==0){
vis[next.x]=true;
q.push(next);
}
}
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(vis,0,sizeof(vis));
printf("%d\n",bfs());
}
}