河内塔的基本版本可以在这里找到。
它是河内问题的扭曲塔。其中,所有规则都是相同的,只是增加了一个规则:
您不能将任何磁盘直接从第一个杆移动到最后一个杆,即,如果要将磁盘从第一个杆移动到最后一个杆,则必须先将第一个杆移动到中间杆,然后再移动到最后一个杆。
方法:
- 基本案例:如果磁盘数量为1,则先将其移动到中间杆,然后再将其移动到最后一根杆。
- 递归案例:在递归案例中,以下步骤将产生最佳解决方案:(所有这些动作都遵循河内塔扭曲问题的规则)
- 我们将首先将n-1个磁盘移动到最后一个磁棒。
- 然后将最大的圆盘移至中间杆。
- 将前n-1个磁盘从最后一个杆移到第一个杆。
- 将最大的圆盘从中间杆移到最后一个杆。
- 将所有n-1个磁盘从第一个杆移动到最后一个杆。
下面是上述方法的实现:
C++
// C++ implementation
#include
using namespace std;
// Function to print the moves
void twistedTOH(int n, char first,
char middle, char last)
{
// Base case
if (n == 1) {
cout << "Move disk " << n
<< " from rod " << first
<< " to " << middle
<< " and then to "
<< last << endl;
return;
}
// Move n-1 disks from first to last
twistedTOH(n - 1, first, middle, last);
// Move largest disk from first to middle
cout << "Move disk " << n
<< " from rod " << first
<< " to " << middle << endl;
// Move n-1 disks from last to first
twistedTOH(n - 1, last, middle, first);
// Move nth disk from middle to last
cout << "Move disk " << n
<< " from rod " << middle
<< " to " << last << endl;
// Move n-1 disks from first to last
twistedTOH(n - 1, first, middle, last);
}
// Driver's Code
int main()
{
// Number of disks
int n = 2;
// Rods are in order
// first(A), middle(B), last(C)
twistedTOH(n, 'A', 'B', 'C');
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to print the moves
static void twistedTOH(int n, char first,
char middle, char last)
{
// Base case
if (n == 1)
{
System.out.println("Move disk " + n + " from rod " +
first + " to " + middle +
" and then to " + last);
return;
}
// Move n-1 disks from first to last
twistedTOH(n - 1, first, middle, last);
// Move largest disk from first to middle
System.out.println("Move disk " + n +
" from rod " + first +
" to " + middle);
// Move n-1 disks from last to first
twistedTOH(n - 1, last, middle, first);
// Move nth disk from middle to last
System.out.println("Move disk " + n +
" from rod " + middle +
" to " + last);
// Move n-1 disks from first to last
twistedTOH(n - 1, first, middle, last);
}
// Driver Code
public static void main(String[] args)
{
// Number of disks
int n = 2;
// Rods are in order
// first(A), middle(B), last(C)
twistedTOH(n, 'A', 'B', 'C');
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 implementation of above approach
# Function to print the moves
def twistedTOH(n, first, middle, last):
# Base case
if (n == 1):
print("Move disk", n, "from rod", first,
"to", middle, "and then to", last)
return
# Move n-1 disks from first to last
twistedTOH(n - 1, first, middle, last)
# Move largest disk from first to middle
print("Move disk", n, "from rod",
first, "to", middle)
# Move n-1 disks from last to first
twistedTOH(n - 1, last, middle, first)
# Move nth disk from middle to last
print("Move disk", n, "from rod",
middle, "to", last)
# Move n-1 disks from first to last
twistedTOH(n - 1, first, middle, last)
# Driver Code
# Number of disks
n = 2
# Rods are in order
# first(A), middle(B), last(C)
twistedTOH(n, 'A', 'B', 'C')
# This code is contributed by
# divyamohan123
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to print the moves
static void twistedTOH(int n, char first,
char middle, char last)
{
// Base case
if (n == 1)
{
Console.WriteLine("Move disk " + n + " from rod " +
first + " to " + middle +
" and then to " + last);
return;
}
// Move n-1 disks from first to last
twistedTOH(n - 1, first, middle, last);
// Move largest disk from first to middle
Console.WriteLine("Move disk " + n +
" from rod " + first +
" to " + middle);
// Move n-1 disks from last to first
twistedTOH(n - 1, last, middle, first);
// Move nth disk from middle to last
Console.WriteLine("Move disk " + n +
" from rod " + middle +
" to " + last);
// Move n-1 disks from first to last
twistedTOH(n - 1, first, middle, last);
}
// Driver Code
public static void Main(String[] args)
{
// Number of disks
int n = 2;
// Rods are in order
// first(A), middle(B), last(C)
twistedTOH(n, 'A', 'B', 'C');
}
}
// This code is contributed by PrinciRaj1992
输出:
Move disk 1 from rod A to B and then to C
Move disk 2 from rod A to B
Move disk 1 from rod C to B and then to A
Move disk 2 from rod B to C
Move disk 1 from rod A to B and then to C
递归公式:
T(n) = T(n-1) + 1 + T(n-1) + 1 + T(n-1)
= 3 * T(n-1) + 2
where n is the number of disks.
通过解决该递归,时间复杂度将为O(3 n ) 。