Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.

book
The Tower of Hanoi

The Tower of Hanoi is a mathematical puzzle consisting of three rods and a set of disks of varying sizes, initially stacked in decreasing order on one rod, forming a conical shape. The objective is to move the entire stack from the first rod to the last while following these rules:

  • Only one disk may be moved at a time.
  • Each move involves taking the top disk from one rod and placing it on another rod or an empty rod.
  • No disk may be placed on top of a smaller disk.

Given a setup with eight disks (sizes 1-8) stacked on three rods (indexed as 0, 1, and 2), where each rod maintains a sorted order from largest at the bottom to smallest at the top, find a sequence of moves [i, j] where i is the index of the rod from which the smallest disk is taken, and j is the index of the rod where it is placed, ensuring all moves remain valid.

Example 1

Input:

Given setup with three disks as example

Output:

[[0, 2], [0, 1], [2, 1], [0, 2], [1, 0], [1, 2], [0, 2]]
main.cpp

main.cpp

C++

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

some-alt