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

One Move from Towers of Hanoi

One Move from Towers of Hanoi

For this problem, we are concerned with the classic problem of Towers of Hanoi. In this problem there are three posts and a collection of circular disks. Let’s call the number of disks \textbf{n}. The disks are of different sizes, with no two having the same radius, and the one main rule is to never put a bigger disk on top of a smaller one. We will number the disks from \textbf{1} (smallest) to \textbf{n} (biggest) and name the posts \textbf{A}, \textbf{B}, and \textbf{C}. If all the disks start on post \textbf{A}, and the goal is to move the disks to post \textbf{C} by moving one at a time, again, never putting a bigger one on top of a smaller one, there is a well-known solution that recursively calls for moving \textbf{n--1} disks from \textbf{A} to \textbf{B}, then directly moves the bottom disk from \textbf{A} to \textbf{C}, then recursively calls for moving the \textbf{n--1} disks from \textbf{B} to \textbf{C}. Pseudocode for a recursive solution to classic Towers of Hanoi problem: \textbf{move(num_disks, from_post, spare_post, to_post)} \textbf{if (num_disks == 0)} \textbf{return} \textbf{move(num_disks -- 1, from_post, to_post, spare_post)} \textbf{print ("Move disk ", num_disks, " from ", from_post, " to ", to_post)} \textbf{move(num_disks -- 1, spare_post, from_post, to_post)} The problem at hand is determining the \textbf{k}^th move made by the above algorithm for a given \textbf{k} and \textbf{n}. \InputFile Input will be two integers per line, \textbf{k} and \textbf{n}. End of file will be signified by a line with two zeros. All input will be valid, \textbf{k }and \textbf{n} will be positive integers with \textbf{k} less than \textbf{2^n} so that there is a \textbf{k}^th move, and \textbf{n} will be at most \textbf{60} so that the answer will fit in a \textbf{64}-bit integer type. \OutputFile Output the requested \textbf{k}^th move made by the above algorithm. Follow this format exactly: "\textbf{Case}", one space, the case number, a colon and one space, and the answer for that case given as the number of the disk, the name of the \textbf{from_post}, and the name of the \textbf{to_post} with one space separating the parts of the answer. Do not print any trailing spaces.
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
1 3
5 3
8 4
0 0
Çıxış verilənləri #1
Case 1: 1 A C
Case 2: 1 B A
Case 3: 4 A C
Mənbə ACM ICPC NCNA Programming Contest - November 7, 2013