移动 II (广搜,记录路径)

移动 II
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 27(14 users) Total Accepted: 16(14 users) Special Judge: No
Description

在坐标轴[0,500]上存在两点A,B。

点A可以多次移动,每次移动需要遵循如下规则:

1.向后移动一步。

2.向前移动一步。

3.跳到当前坐标*2的位置上。

要求:利用宽搜算法编程求解从A移动到B的步数最少的方案,为使答案统一,要求搜索按照规则1、2、3的顺序进行。

Input

输入包含多组测试用例。

每组测试用例要求输入两个整数A,B。

Output

按要求输出步数最少的方案。

向后走输出"step back"。

向前走输出"step forward"。

跳跃输出"jump"。

对于每组结果需要追加一个空行。

Sample Input
5 17
5 18
3 499
Sample Output
step back
jump
jump
step forward

jump
step back
jump

step forward
jump
jump
jump
step back
jump
jump
step forward
jump
jump
step back

Source
2012 Spring Contest 4 - Search Technology
Author

卢俊达

http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1316

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int q[100000];
int cl,op;
int A,B;
int used[1000];
int root[1000];
void bfs()
{
memset(used,0,sizeof(used));
cl=op=0;
q[op++]=A;
int tmp;
used[A]=1;
while(cl<op)
{
tmp=q[cl++];

if(tmp==B)
{
break;
}
if(used[tmp-1]==0&&tmp-1>=0&&tmp-1<=600)
{
root[tmp-1]=tmp;
used[tmp-1]=1;
q[op++]=tmp-1;
}
if(used[tmp+1]==0&&tmp+1>=0&&tmp+1<=600)
{
root[tmp+1]=tmp;
used[tmp+1]=1;
q[op++]=tmp+1;
}
if(used[tmp*2]==0&&tmp*2>=0&&tmp*2<=600)
{
root[tmp*2]=tmp;
used[tmp*2]=1;
q[op++]=tmp*2;
}
}
}

void print(int i)
{
if(i==A)
{

return ;
}
print(root[i]);

if(i==root[i]-1)printf("step back\n");

else if(i==root[i]+1)
printf("step forward\n");
else printf("jump\n");

}
int main()
{
// freopen("test.out","w",stdout);
while(scanf("%d%d",&A,&B)!=EOF)

{


bfs();
print(B);
printf("\n");
}
return 0;
}
原文地址:https://www.cnblogs.com/kuangbin/p/2417024.html