![]() However, the catch is, a larger disc can never sit on top of a smaller disc. The aim is to move the tower, one disc at a time, over to the right-hand pole. There are three poles in a row, the one on the left containing a series of discs of decreasing size, with the other two, empty. In each case, a total of 2n-1 moves are made.In 1883, a French mathematician named Édouard Lucas came up with an intriguing scenario. * make the legal move between pegs B and C * make the legal move between pegs A and C * make the legal move between pegs A and B uneven : move to the left, even: move to the right Locate second biggest disc (B), smallest stack = C -> move C to B Shortest code (but non-optimal moves), tracking position of disks rather than list of disks on rods (idea shamelessly stolen from the Perl solution) val r=args(0).split(",",-1) var d=Map) c=t if c=1 then one=k end end - locate smallest disc, and set index for nonexistant discs to 1e9 I don't know of any issues, and I have no other ideas for further reducing the movesīits of this are quite elegant (IMO) but other bits are an ugly hack Sub a starter for 10, in Scala, revised a few times. With the extra 6 chars for the command-line switch, this gets us down to net 203 characters: # invoke as perl -apF. It could be removed (12 characters) and the output would still be valid.Īnother 12 characters can be removed if the perl interpreter is invoked with the command-line switch -apF. Appends to $_ and sets substitution inside sub M optimizes the output, removing extraneous steps. Rewritten to keep track of the location of each disk as opposed to the list of disks that are contained on each rod.ģ06 291 263 244 236 213 209 chars after removing unnecessary whitespace. It will run a simulation, at speed which you can also change, with a visual representation, printing out any errors in the bottom part. To use it, paste the input to the program to the INPUT field, and the output produced by your program to the PROCESS field. Here it is: Hanoi AlgoTest (wait for it to load then refresh - Dead link :|) So, I prepared this small flash for you, with which you can test if your program produced a good solution without writing it down or anything. Make sure the solution is as fast as possible (as little turns as possible) - that is, don't waste turns by "12,21,12". So it can if the input is not valid (like bigger disk on a smaller one, missing disk, unsolvable). If the input is not in this formatted like that, your program can produce undefined behavior. The input can have an empty rod(s), like these: 2-1,3, If the input IS in the original state, just put the disks to a DIFFERENT rod. Your program can NOT produce null output. The input does not have to describe a field in "original" state - it can be mid-solved. There are only 3 rods, so there is just 6 possible combinations (because a disk has to be moved to another rod, not the same one): 12 The output of your program should look like this: 12,23,31,12,23,13Ī list of numbers, separated by commas that defines the rod that a disk should be taken of, and the rod that the disk should be put on. Īs you can see in the above representation - the the left-most part of the input is rod number one, the middle is rod number two, and the last one is rod number 3. So - for the above input, this would be a visual representation. They are separated too, this time with hyphens ( - ), and each subpart is a number, the larger the number is, the larger the disk is. The parts are a list of disks on each of the 3 rods. Input is a string, consisting of 3 parts, separated by commas. YOUR goal will be to make a program in programming language of your choice, that takes an input (described below) and outputs the steps necessary to solve the position.Īs always, try to make it as short as possible. The GOAL of the game is to move the original "stack" of disks on another rod, that is - put all of the disks on another rod, so (again) the largest is on the bottom, and the smallest on the top Implementation a rod, with x disks, sorted so the largest is on the bottom, and the smallest on the top.a disk can be moved somewhere, ONLY if the top-most disk at the target rod is bigger than the one to be movedĪnd finally - the play field STARTS like this:.the disk must be taken from the top of a rod.only one disk can be moved at once, and it must be moved on the top of another rod.The disks can be put on the rod, with these RULES: The play field consists of 3 rods, and x number of disks, each next one bigger than the previous one. The Towers of Hanoi is a puzzle, and if you are not very familiar with it, here is how it works: It is not currently accepting new answers or interactions. ![]() This question and its answers are locked because the question is off-topic but has historical significance.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |