-
总时间限制:
- 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<cmath>
#include<cstring>
#include<cstdio>

博客探讨了跳房子游戏的规则,其中玩家根据石头落在房屋(H)或外面(O)的位置决定跳跃到哪个房屋。游戏设定在x轴上的连续正整数坐标上进行,每个坐标表示一个房屋。新规则规定,如果石头落在房屋内,玩家可以跳跃到当前房屋的三倍位置;如果落在外面,可以跳跃到当前房屋的一半位置。问题在于计算从源房屋(n)到目标房屋(m)所需的最少跳跃次数,并给出相应的石头投掷序列。如果有多种方式,应输出字母顺序最小的序列。
最低0.47元/天 解锁文章
556

被折叠的 条评论
为什么被折叠?



