towers-of-hanoi

Tower of Hanoi using recursion

∥☆過路亽.° 提交于 2019-12-03 21:59:30
I have no idea about Tower of Hanoi . I want to write a program on this using recursion. Simone Carletti From Wikipedia : The Tower of Hanoi or Towers of Hanoi (also known as The Towers of Brahma) is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks neatly stacked in order of size on one rod, the smallest at the top, thus making a conical shape. Check out the recursive solution . Another homework assignment. Pass your teacher's A to me ;) Source: http://www.soc.napier.ac.uk/~andrew

Facebook sample puzzle: Towers of Hanoi

匆匆过客 提交于 2019-12-03 14:10:58
Here is a question from Facebook hiring sample test. There are K pegs. Each peg can hold discs in decreasing order of radius when looked from bottom to top of the peg. There are N discs which have radius 1 to N; Given the initial configuration of the pegs and the final configuration of the pegs, output the moves required to transform from the initial to final configuration. You are required to do the transformations in minimal number of moves. A move consists of picking the topmost disc of any one of the pegs and placing it on top of any other peg. At any point of time, the decreasing radius

Code Golf: Towers of Hanoi

梦想的初衷 提交于 2019-12-03 05:04:43
Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. Learn more . Rules The Towers of Hanoi is a puzzle, and if you are not very familiar with it, here is how it works: The play field consists of 3 rods, and x number of disks, each next one bigger than the previous one. The disks can be put on the rod, with these RULES : only one disk can be moved at once, and it must be moved on the top of another rod the disk must be taken from the top of a rod a disk can be moved

Towers Of Hanoi Java

北城以北 提交于 2019-12-02 10:40:10
This is a homework that I was working on. I have created 2 classes to play Towers of Hanoi. The first one is the basically a runner to run the actual game class. import java.util.Scanner; class TowersRunner { public static void main(String[] args) { TowersOfHanoi towers = new TowersOfHanoi(); towers.TowersOfHanoi() } } public class TowersOfHanoi { public static void main(String[] args) { System.out.println("Please enter the starting " + "number of discs to move:"); Scanner scanner = new Scanner(System.in); int num_of_discs = scanner.nextInt(); solve(num_of_discs, 'A', 'B', 'C'); } public

Crockford's hanoi function (from “The Good Parts”) [duplicate]

。_饼干妹妹 提交于 2019-12-01 05:27:19
This question already has an answer here: How does recursive algorithm work for Towers of Hanoi? 2 answers At the moment I'm reading Douglas Crockford's book, and the towers of hanoi function is a bit over my head. Even with logging stuff to the console I wasn't able to really understand what's going on. Here's the function with my additions: var hanoi = function (disc, src, aux, dst) { console.log(disc); console.log(src, dst); if (disc > 0) { hanoi(disc - 1, src, dst, aux); console.log('Move disc ' + disc + ' from ' + src + ' to ' + dst); hanoi(disc - 1, aux, src, dst); } } hanoi(3, 'Src',

Towers of Hanoi Python - understanding recursion [duplicate]

守給你的承諾、 提交于 2019-12-01 04:44:17
问题 This question already has answers here : Tower of Hanoi: Recursive Algorithm (25 answers) Closed 5 years ago . I'm completely new to Python and I am currently going over a tutorial about The Towers of Hanoi and recursion. I thought that I understood recursion until they gave this example: def moveTower(height,fromPole, toPole, withPole): if height >= 1: moveTower(height-1,fromPole,withPole,toPole) moveDisk(fromPole,toPole) moveTower(height-1,withPole,toPole,fromPole) #print(withPole) def

Hanoi Tower(Towers of Hanoi)

帅比萌擦擦* 提交于 2019-11-29 11:57:09
I'm trying to do the Towers of Hanoi problem, what I have tried so far: move(1,[H|T],B,C,A1,B1,C) :- A1 = T, B1 = [H|B]. move(N,A,B,C,A1,B1,C) :- N>1, M is N-1, move(M,[H|T],C,B,A1,B1,C), move(1,[H|T],B,_,A1,B1,C), move(M,C,B,[H|T],A1,B1,C). but this code does not work, I need get the result is looks like this: ?-move(3,[1,2,3],[],[],A1,B1,C). and the results: A1=[]. B1=[1,2,3] C=[]. can someone help me fix my code up and can get the result like that? This is very important for me, I really need help. this is what i did but with some problems: move(N,[H|T],[],[],A1,B1,C) :- N > 1, M is N - 1,

How does recursive algorithm work for Towers of Hanoi?

我是研究僧i 提交于 2019-11-27 11:37:10
This is code from a book I have explaining recursion. The problem is that I don't understand the steps taken by the program: var hanoi = function(disc,src,aux,dst) { if (disc > 0) { hanoi(disc - 1,src,dst,aux); document.write("Move disc " + disc + " from " + src + " to " + dst + "<br />"); hanoi(disc - 1,aux,src,dst); } }; hanoi(3,"src","aux","dst"); This is how the Output reads: Move disc 1 from src to dst Move disc 2 from src to aux Move disc 1 from dst to aux Move disc 3 from src to dst Move disc 1 from aux to src Move disc 2 from aux to dst Move disc 1 from src to dst Can someone break

How does this work? Weird Towers of Hanoi Solution

无人久伴 提交于 2019-11-27 02:40:16
I was lost on the internet when I discovered this unusual, iterative solution to the towers of Hanoi: for (int x = 1; x < (1 << nDisks); x++) { FromPole = (x & x-1) % 3; ToPole = ((x | x-1) + 1) % 3; moveDisk(FromPole, ToPole); } This post also has similar Delphi code in one of the answers. However, for the life of me, I can't seem to find a good explanation for why this works. Can anyone help me understand it? Antti Huima the recursive solution to towers of Hanoi works so that if you want to move N disks from peg A to C, you first move N-1 from A to B, then you move the bottom one to C, and

How does recursive algorithm work for Towers of Hanoi?

痴心易碎 提交于 2019-11-26 15:39:22
问题 This is code from a book I have explaining recursion. The problem is that I don't understand the steps taken by the program: var hanoi = function(disc,src,aux,dst) { if (disc > 0) { hanoi(disc - 1,src,dst,aux); document.write("Move disc " + disc + " from " + src + " to " + dst + "<br />"); hanoi(disc - 1,aux,src,dst); } }; hanoi(3,"src","aux","dst"); This is how the Output reads: Move disc 1 from src to dst Move disc 2 from src to aux Move disc 1 from dst to aux Move disc 3 from src to dst