【记录路径的BFS】 跳房子

题目来源:百练 跳房子

C:Hopscotch
查看 提交 统计 提问
总时间限制: 1000ms 内存限制: 65536kB
描述
Hopscotch(跳房子) is a popular game. The rule is very simple: we draw some houses on the ground and then throw a stone. Based on the position of the stone, we decide which house to jump to. Here, we use consecutive positive integer coordinates on the x-axis to represent the houses,the coordinate of the ith house is i. We also assume that there are infinite number of houses.

Then, we define the new rules as follows, each time we throw a stone,

  1. if the stone falls into a house (marked as H), we can jump from the current house i to the house 3*i;

  2. if the stone falls outside the house (marked as O), we can jump from the current house i to house i/2.(round down).

For example, initially at house 1, and in order to go to house 6, we can throw the stones in two ways:HHHOO or HHOHO.

Now,your task is to compute the least number of jumps (k) needed to jump from a source house (n) and a destination house (m). You should also output the corresponding way of throwing the stones. If there are multiple ways, please output the smallest way in alphabet order.

输入
There are multiple test cases.
For each test case, there are two integers n and m (1<=n, m<=1000), representing the source house and destination house. The input ends with 0 0.
输出
For each test case, the first line of output is an integer, which is the minimal number of jumps (k). The testing data guarantees that there is a solution and k <=25. The second line outputs the corresponding way of throwing the stone.

样例输入
1 6
0 0
样例输出
5
HHHOO

提示
For the sample input, the minimal number of jump is 5 (k), and correspondingly there are two ways of throwing stones,
1->H->3->H->9->H->27->O->13->O->6
1->H->3->H->9->O->4->H->12->O->6
We choose the first one as it is smaller in alphabet order.

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
using namespace std;
int n, m;
struct node
{
    int index;
    int step;
    vector<char> s; //记录路径
};
void BFS()
{
    map<int, bool> mp;
    queue<node> q;
    node temp;
    temp.index = n;
    temp.step = 0;
    q.push(temp);
    while (!q.empty())
    {
        node top = q.front();
        q.pop();
        for (int i = 0; i < 2; i++)
        {
            node temp = top;
            if (i == 0)
            {
                temp.index = top.index * 3;
                temp.step = top.step + 1;
                if (!mp[temp.index])
                {
                    temp.s.push_back('H');
                    q.push(temp);
                    mp[temp.index] = true;
                }
            }
            else
            {
                temp.index = top.index / 2;
                temp.step = top.step + 1;
                if (!mp[temp.index])
                {
                    temp.s.push_back('O');
                    q.push(temp);
                    mp[temp.index] = true;
                }
            }
            if (temp.index == m)
            {
                int len = temp.s.size();
                cout << len << endl;
                for (int i = 0; i < len; i++)
                {
                    cout << temp.s[i];
                }
                cout << endl;
                return;
            }
        }
    }
}

int main()
{
    while (cin >> n >> m)
    {
        if (n == 0 && m == 0)
        {
            break;
        }
        BFS();
    }
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值