hrust OJ 1316 移动II

移动 II
Time Limit: 1000 MSMemory Limit: 65536 K
Total Submit: 107(29 users)Total Accepted: 59(28 users)Rating: 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

//广搜+路径记录

#include<iostream>
#include<map>
#include<queue>
#include<string.h>
using namespace std;

int mark[505];
int root[505];
int A, B;

void bfs()
{
    queue<int>J;
    int temp;
    J.push(A);
    mark[A]=1;
    while(J.size())
    {
        temp=J.size();
        while(temp--)
        {
            int next=J.front();
            if(next==B) return ;
            if(next-1<=500&&next-1>=0&&mark[next-1]==0)
            {
                root[next-1]=next;
                J.push(next-1);
                mark[next-1]=1;
            }
            if(next+1>=0&&next+1<=500&&mark[next+1]==0)
            {
                root[next+1]=next;
                J.push(next+1);
                mark[next+1]=1;
            }
            if(2*next>=0&&2*next<=500&&mark[2*next]==0)
            {
                root[2*next]=next;
                J.push(2*next);
                mark[2*next]=1;
            }
            J.pop();
        }
    }
}

void print(int i)
{
    if(i==A) return ;
    print(root[i]);
    if(i==root[i]-1) cout << "step back"  << endl;
    else if(i==root[i]+1) cout << "step forward" << endl;
    else  cout << "jump" << endl;
}

int main()
{
      while(cin >> A >> B)
      {
          memset(mark, 0, sizeof(mark));
          bfs();
          print(B);
          cout << endl;
      }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值