Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按照给定的安排解决河内塔问题_Java_Algorithm_Recursion_Towers Of Hanoi - Fatal编程技术网

Java 按照给定的安排解决河内塔问题

Java 按照给定的安排解决河内塔问题,java,algorithm,recursion,towers-of-hanoi,Java,Algorithm,Recursion,Towers Of Hanoi,我得到了一个有效的河内磁盘塔的安排。排列以数组形式给出 例如[2,2,0]:数组的索引是磁盘的标识符,从小到大排序,数组的值是相应磁盘的位置(位置始终为0、1或2)。在[2,2,0]的情况下,最小的两个磁盘位于第三极,而最大的磁盘位于第一极: | | | | | [0] [22222] | [111] ----+---- ----+---- --

我得到了一个有效的河内磁盘塔的安排。排列以数组形式给出

例如[2,2,0]:数组的索引是磁盘的标识符,从小到大排序,数组的值是相应磁盘的位置(位置始终为0、1或2)。在[2,2,0]的情况下,最小的两个磁盘位于第三极,而最大的磁盘位于第一极:

      |           |           |
      |           |          [0]
   [22222]        |         [111] 
  ----+----   ----+----   ----+----
另一个例子:[0,2,1]

      |           |           |
      |           |           |
     [0]       [22222]      [111] 
  ----+----   ----+----   ----+----
对于将所有磁盘移动到目标磁极(第二极)所需的其余步骤,是否可以递归地解决该问题


我没有一个递归的解决方案给你。当您查看河内塔的常用递归算法时,实际状态可能发生在递归树的深处,如果您想象这样一个状态被传递给您的函数,那么从该状态进一步解决问题不仅需要递归调用,还需要重建“外部”递归调用堆栈。这似乎使它变得相当复杂

但是您可以迭代地进行。以下是一种方法:

static void solveForSteps(int[]磁盘){
int n=磁盘长度;
//计算每个磁盘的下一个目标棒
int target=2;//最大的磁盘应该放在最右边的棒上
int[]目标=新的int[n];
对于(int i=n-1;i>=0;i--){
目标[i]=目标;
如果(磁盘[i]!=目标){
//为了允许这种移动,较小的磁盘需要让开
目标=3-目标-磁盘[i];
}
}
int i=0;
而(i=0;j--){
目标[j]=目标;
目标=3-目标-磁盘[j];
}
打破
}
}
}
}

这将打印将所有磁盘移动到最右边磁极所需的其余移动。相反,如果您想以中心杆为目标,则只需将
int target=2
更改为
int target=1

即可从任意位置求解河内塔,您可以使用类似于从标准起始位置工作的标准解的递归过程

它只是需要更一般一点


编写一个递归过程moveDisks(maxSize,targetPeg)以大小移动所有磁盘。请查看是否在如此混乱的页面上发布问题。尽量把问题说得更清楚。杆子和钉子的意思是一样的。在河内的塔楼里,只有三根杆子。光盘是移动的东西。考虑到这一点,请澄清您的问题并解释您的数组表示法好吗?
public int solveForSteps(int[] disks){
    // Is there a possible way to solve with given arrangements?
}
/**
 * Solve towers of hanoi from an arbitrary position
 * 
 * @param diskPositions the current peg for each disk (0, 1, or 2) in increasing
 *                      order of size.  This will be modified
 * @param disksToMove  number of smallest disks to moves
 * @param targetPeg target peg for disks to move
 */
static void moveDisks(int[] diskPositions, int disksToMove, int targetPeg)
{
    for (int badDisk = disksToMove-1; badDisk >= 0; --badDisk) {
        int currentPeg = diskPositions[badDisk];
        if (currentPeg != targetPeg) {
            // found the largest disk on the wrong peg

            // sum of the peg numbers is 3, so to find the other one:
            int otherPeg = 3 - targetPeg - currentPeg;

            // before we can move badDisk, we have to get the smaller
            // ones out of the way
            moveDisks(diskPositions, badDisk, otherPeg);

            // Move
            diskPositions[badDisk] = targetPeg;
            System.out.println(
                "Move " + badDisk + " from " + currentPeg + " to " + targetPeg
            );

            //Now we can put the smaller ones in the right place
            moveDisks(diskPositions, badDisk, targetPeg);
            break;
        }
    }
}