towers-of-hanoi

Towers of Hanoi question

人盡茶涼 提交于 2019-12-23 03:05:06
问题 I read through a few of the discussions about the Towers of Hanoi problem. I understand the recursive solution using the following code: void Hanoi3(int nDisks, char source, char intermed, char dest) { if(nDisks == 1){ cout << "Move the plate from " << source << " to " << dest << endl; } else{ Hanoi3(nDisks - 1, source, dest, intermed); cout << "Move the plate from " << source << " to " << dest << endl; Hanoi3(nDisks - 1, dest, intermed, source); } } What I actually need to do is output some

Towers of Hanoi in Scheme (recursive)

妖精的绣舞 提交于 2019-12-22 18:30:20
问题 I wrote the following code in scheme today, but the evaluation is wrong. Please don't tell me I suck at programming, I understand that this is a classic recursion problem, but I am having trouble with it: (define (towers-of-hanoi n source temp dest) (if (= n 1) (begin (display "Move the disk from ") (display source) (display " to " ) (display dest) (newline)) (begin (towers-of-hanoi (- n 1) source temp dest) (display "Move the disk from ") (display source) (display " to ") (display dest)

Towers Of Hanoi Java

五迷三道 提交于 2019-12-20 06:39:22
问题 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

Using pygame.time.wait() between display updates

别等时光非礼了梦想. 提交于 2019-12-19 10:48:13
问题 I am currently developing a simple Tower of Hanoi animation in Pygame, that should show the correct solution of Tower of Hanoi, moving one piece per second. However, in my hanoi solving algorithm, I'm trying to update the display and use pygame.time.wait() after each movement; and instead of updating one movement and waiting one second, the program waits the total number of movements amount of seconds and then displays the tower with all the movements done at once. What I would like to know

Using pygame.time.wait() between display updates

↘锁芯ラ 提交于 2019-12-19 10:47:01
问题 I am currently developing a simple Tower of Hanoi animation in Pygame, that should show the correct solution of Tower of Hanoi, moving one piece per second. However, in my hanoi solving algorithm, I'm trying to update the display and use pygame.time.wait() after each movement; and instead of updating one movement and waiting one second, the program waits the total number of movements amount of seconds and then displays the tower with all the movements done at once. What I would like to know

How does this iterative Tower of Hanoi work? C [duplicate]

Deadly 提交于 2019-12-18 16:50:30
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: How does this work? Weird Towers of Hanoi Solution While surfing Google, i found this interesting solution to Tower Of Hanoi which doesn't even use stack as data structure. Can anybody explain me in brief, what is it actually doing? Are this solution really acceptable? Code #include <stdio.h> #include <stdlib.h> int main() { int n, x; printf("How many disks?\n"); scanf("%d", &n); printf("\n"); for (x=1; x < (1 <

Facebook sample puzzle: Towers of Hanoi

不问归期 提交于 2019-12-14 00:17:12
问题 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

Solving recursive Towers of Hanoi in Lisp

◇◆丶佛笑我妖孽 提交于 2019-12-13 02:18:32
问题 My code in lisp is as follows: (defun solve-hanoi(from) (hanoi (length from) from '() '())) (defun hanoi(height from to aux) (when (>= height 1) (hanoi (- height 1) from aux to) (format t "~%Move ~a from ~a to ~a" (nth 0 from) from to) (push (pop from) to) (hanoi (- height 1) aux to from))) I am new to lisp and have NO clue as to what I am doing wrong. Help with this would be GREATLY appreciated since I've been at this for hours. Thanks. 回答1: The recursive algorithm is: To move n discs from

Tower of Hanoi using recursion

此生再无相见时 提交于 2019-12-12 08:17:34
问题 I have no idea about Tower of Hanoi. I want to write a program on this using recursion. 回答1: 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. 回答2: Another

How can I instantiate an array of Stacks of type int?

依然范特西╮ 提交于 2019-12-12 03:57:40
问题 I am trying to create an array of stacks, in which each stack within the array is of type int . If I create the array like this: Stack<Integer>[] numbers = new Stack<Integer>[3]; , there is the compile error " Cannot create a generic array of Stack<Integer> ". So, I am trying to create the array of Stacks with the wildcard type instead of Integer , and it then does not have this error. However, if I then try to push an int into one of the stacks (of wildcard " ? " type) like this: this