When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

turbozone88's blog

By turbozone88, history, 16 months ago, In English

Tower of Hanoi game is a famous game with discs of different sizes, with a hole in the middle, located on three stakes A, B, C.

The game begins with the state that the disks are stacked on pole A, the small disk is on the large disk, ie the smallest disk is on top, creating a cone shape. The requirement of the game is to transfer all the discs from pole A to pole C, following the following rules:

Only use 3 stakes to transfer.

Only one top disk can be moved at a time from one pole to another.

A disc is only placed on a larger disk.

In this problem, we will have n disks, numbered from 1 to n in order of increasing disk size. Initially, the disks are scattered on three poles but still satisfy the condition that the small disk is on the large disk and the goal is to convert the entire disk into a stack of disks at pole C.

Requirements: Given the state that the disks are on the poles, find a way to convert the entire disk into a stack of disks on pole C.

The first line contains the positive integer n.

The second line contains a string of n characters, the number of which is 'A' or 'B' or 'C' indicating whether disk i is on pole A or pole B or peg C.

limit : n <= 20

Input : 3 AAC

Output : 3 A->B A->C B->C

Someone help me please !

  • Vote: I like it
  • +2
  • Vote: I do not like it

| Write comment?
»
15 months ago, # |
  Vote: I like it 0 Vote: I do not like it

This is a standard problem. You can find like ten trillion solutions for it online. If you are looking for a judge to check if your implementation is correct or not, then here you go: Ha Noi Tower — CSES