简体   繁体   English

河内之塔:找到第n个配置

[英]Towers of Hanoi: Finding the n-th configuration

I want to find the n-th configuration in the solution of the Towers of Hanoi problem given the number of discs and the move's number. 考虑到光盘数量和移动数量,我想在河内塔问题的解决方案中找到第n个配置。

The following code finds the n-th move using tail recursion: 以下代码使用尾部递归找到第n个移动:


  public static String N_th_Move(int k_discs, int move){
        return HanoiRec(k_discs, move, "A", "B", "C");
    }

    private static String HanoiRec(int k_discs, int move, String rod_a, String rod_b, String rod_c) {
        int max_n_moves = (int) (Math.pow(2, k_discs) - 1); 
        int bound =(int) Math.pow(2, k_discs - 1);
        if(move > max_n_moves){
            return "Not valid";
        } else if(move == bound ){
            return rod_a + " -> " + rod_b;
        } else if(move < bound){
            return HanoiRec(k_discs-1, move , rod_a, rod_c, rod_b);
        } else {
            return HanoiRec(k_discs-1, move - bound, rod_c, rod_b, rod_a);
        }
    }


How to find the n-th configuration using the same approach? 如何使用相同的方法找到第n个配置?

Eg: 例如:

N_th_configuation(3, 4) #{rod_a: 0, rod_b: 1, rod_c: 2}

ADDED: The binary tree for 3 discs (following the above code): 添加:3张光盘的二进制树(按照上面的代码):


                                (0  1  2)
                               /         \
                        (1 1 1)           (0 2 1)
                       /       \         /       \
                    (2 1 0)  (1 0 2)  (1 1 1)  (0 3 0)

Where the first number is the number of discs on rod_a, the second on rod_b and the third on rod_c. 其中第一个数字是rod_a上的光盘数量,第二个是rod_b上的光盘数量,第三个是rod_c上的光盘数量。 The bottom-left leaf is the configuration after the first move and the bottom-right leaf is the configuration after the last move. 左下方的叶子是第一次移动后的配置,右下方的叶子是最后一次移动后的配置。 I don't find out the relation between all configurations. 我没有找到所有配置之间的关系。

The canonical solution for ToH is to alternate two types of moves: ToH的规范解决方案是交替两种类型的移动:

  1. Move the smallest disc to the next rod (with wraparound back to the initial rod) 将最小的圆盘移至下一个杆(环绕式移回到初始杆)
  2. Make the one legal move that does not include the smallest disc. 采取包含最小光盘的合法措施。

wlog (without loss of generality), let's assume that the smallest disc always moves to the next higher-numbered rod (labeled 0, 1, 2). wlog(不失一般性),我们假设最小的光盘始终移动到下一个编号更高的杆(标记为0、1、2)。

One result of this algorithm is that odd-numbered discs move higher; 该算法的结果是奇数个光盘移动得更高。 even-numbered discs move lower. 偶数编号的碟片下降得更低。

Another result is that you can independently determine the disc for any given move number: it's the lowest-value 1 bit in the binary representation of that number. 另一个结果是,您可以为任何给定的移动编号独立确定光盘:它是该编号的二进制表示形式中的最低值1位。 For instance, for the 3-disc problem: 例如,对于三碟问题:

Move  binary disc
  1     001    1
  2     010    2
  3     011    1
  4     100    3
  5     101    1
  6     110    2
  7     111    1

To find the position matching any move N : 要找到与任何移动N相匹配的位置:

  1. Divide the binaries into separate digits. 将二进制文件分成单独的数字。
  2. Mask off all bit the right-most 1 bit in each. 屏蔽掉所有位最右侧的1在每个位。
  3. Add the columns. 添加列。
  4. Negate the even-numbered columns (to show that the discs move in the opposite direction. 否定偶数列(以表明光盘朝相反方向移动)。
  5. Reduce the totals modulus 3. 降低总模量3。

The result is a list of columns on which each disc rests. 结果是每个光盘都位于其上的列的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM