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. Procedure Hanoi (A, B, C: integer; N:integer); Begin If N>0 then Begin Hanoi (A, C, B, N-1); Writeln('Disk ', N, ' from ', A, ' to ', B); Hanoi (C, B, A, N-1) End End; It is well known that the solution given above requires \textbf{(2^n--1)} steps. Taking into account the initial disposition we totally have \textbf{2^n} combinations of \textbf{n} disks disposition between three pivots. Thus, some combinations don’t occure during the algorithm execution. For example, the combination "\textbf{CAB}" will not be reached during the game with \textit{\textbf{n}} = \textbf{3} (herein the smallest disk is on pivot \textbf{C}, the medium one is on pivot \textbf{A}, the biggest one is on pivot \textbf{B}). Write a program that establishes if the given combination is occurred during the game. \InputFile The first line of an input file contains a single integer \textbf{n} (\textbf{1} ≤\textit{ }n\textit{ }≤ \textbf{250}) -- the number of disks, and the second line 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 lagest, and so on… \OutputFile The output file must contain "\textbf{YES}" or "\textbf{NO}" depending on the reachability of the disks disposition during the game.
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
3
ACB
Çıxış verilənləri #1
YES
Mənbə ACM Central Region of Russia Quarterfinal Programming Contest, Rybinsk, October 17-18-2012