eolymp
bolt
Try our new interface for solving problems
Məsələlər

Hanoi tower

Hanoi tower

Each programmer knows the puzzle "The Hanoi Tower". We will briefly remind you the conditions of this task. There are \textbf{3} pivots: \textbf{A}, \textbf{B}, \textbf{C}. Initially, \textbf{n} disks of different diameter are placed on the pivot \textbf{A}: the smallest disk is placed on the top and every next one is placed in an increasing order of their diameters. The second and the third pivots are still empty. You have to move all the disks from pivot \textbf{A} to pivot \textbf{B}, using pivot \textbf{C} as an auxiliary. By one step you can take off \textbf{1} upper disk and put it either on an empty pivot or on another pivot over a disk with a bigger diameter. Almost all books on programming contain a recursive solution of this task. In the following example you can see the procedure, written in Pascal. \textbf{Procedure Hanoi (A, B, C: integer; N:integer);} \textbf{Begin} \textbf{If N>0 then} \textbf{Begin} \textbf{Hanoi (A, C, B, N-1);} \textbf{Writeln(‘Disk ’, N, ‘ from ‘, A, ‘ to ‘, B);} \textbf{Hanoi (C, B, A, N-1)} \textbf{End} \textbf{End;} Here, for example, the combination "\textbf{BСA}" indicate that the smallest disk is on pivot \textbf{B}, the medium one is on pivot \textbf{С}, the biggest one is on pivot \textbf{A}. The members of jury, in preparation for the championship, played this exciting game, from time to time making notes of current ​​combinations (each on a separate sheet of paper). However, later, the sheets with recorded combinations were mixed. Write a program that establishes if the given combination is occurred during the game. \InputFile The first line of an input file contains two integer \textbf{n} (\textbf{1} ≤ \textbf{n} ≤ \textbf{250}) -- the number of disks, and \textbf{m} (\textbf{1} ≤ \textbf{m} ≤ \textbf{1000}) -- the number of combinations. The following \textbf{m} lines contain \textbf{m} combinations. Each combination contains \textbf{n} capital letters ("\textbf{A}", "\textbf{B}" or "\textbf{C}") -- the disposition of the \textbf{n} disks between the pivots. The first (leftmost) letter designates position (a pivot name) of the smallest disk, the second letter -- position of the second largest, and so on… \OutputFile The output file must contain \textbf{m} lines -- \textbf{m} combinations sorted in order of their appearing in game.
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
3 4
ACB
BCA
ABB
BAA
Çıxış verilənləri #1
BAA
BCA
ACB
ABB
Mənbə 2013-2014 ACM Central Region of Russia Quarterfinal, Rybinsk 2013/10/17