towers-of-hanoi

In need of explanation of how my Tower of Hanoi Recursion code works

南楼画角 提交于 2019-12-12 03:54:39
问题 I am just getting into recursion and I think I have a basic understanding of how it works. I have this code for a Tower of Hanoi problem and I have been staring at it for an hour trying to figure out exactly what it is doing. The method 'moveDisks' is confusing to me. I was hoping someone could help explain what's going on in the method. I didn't write it. To try to understand I ran the code and put 2 for the number of disks. Here is what printed out: The moves are: Move disk 1 from A to C,

How to use new scala syntax to rewrite “TowersOfHanoi”

空扰寡人 提交于 2019-12-11 10:49:56
问题 The code is coming from https://gist.github.com/jrudolph/66925: object TowersOfHanoi { import scala.reflect.Manifest def simpleName(m:Manifest[_]):String = { val name = m.toString name.substring(name.lastIndexOf('$')+1) } trait Nat final class _0 extends Nat final class Succ[Pre<:Nat] extends Nat type _1 = Succ[_0] type _2 = Succ[_1] type _3 = Succ[_2] type _4 = Succ[_3] case class Move[N<:Nat,A,B,C]() implicit def move0[A,B,C](implicit a:Manifest[A],b:Manifest[B]):Move[_0,A,B,C] = { System

Scheme: Towers of Hanoi (recursion)

我只是一个虾纸丫 提交于 2019-12-11 10:43:12
问题 I'd like to start off by saying this is homework; so I'm not asking for a solution, just some tips. I've been ruminating on this for about a week now. Every solution I come up with doesn't do it recursively, seeing as I can't manage to wrap my head around doing it recursively with the list as the only parameter. My professor said they were able to do it with about 6 helper functions. As mentioned, I have to solve the problem taking taking the list as the only parameter. Here's an example of

Towers of Hanoi - giving help to the user mid game

安稳与你 提交于 2019-12-10 12:13:40
问题 I have successfully built the Tower of Hanoi game in java with user interactivity. However, I also want to add a feature where if the user asks for hint, the program does the next move for the user in the most efficient way. I know the game can be solved using recursion but as far as I know, I can't use recursion mid-game to give answers. Do you guys have any suggestions? 来源: https://stackoverflow.com/questions/21668424/towers-of-hanoi-giving-help-to-the-user-mid-game

Tower of Hanoi, stop sliding

为君一笑 提交于 2019-12-08 12:20:49
问题 I developed a solution for the Tower of Hanoi problem: public static void bewege(int h, char quelle, char ablage, char ziel) { if(h > 0){ bewege(h - 1, quelle, ziel, ablage); System.out.println("Move "+ h +" from " + quelle + " to " + ziel); bewege(h - 1, ablage, quelle, ziel); } } It works fine. Now i want to limit the number of slides and throw an exception if a certain limit is reached. I tried it with a counter but it does not work: class HanoiNK{ public static void main(String args[]){

Modified Tower of Hanoi

感情迁移 提交于 2019-12-07 10:08:50
问题 We all know that the minimum number of moves required to solve the classical towers of hanoi problem is 2 n -1. Now, let us assume that some of the discs have same size. What would be the minimum number of moves to solve the problem in that case. Example, let us assume that there are three discs. In the classical problem, the minimum number of moves required would be 7. Now, let us assume that the size of disc 2 and disc 3 is same. In that case, the minimum number of moves required would be:

Towers of Hanoi question

一曲冷凌霜 提交于 2019-12-06 22:18:26
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 type of "illustration" of the towers at each step. I'm having a lot of trouble accomplishing this. Our

Towers of Hanoi in Scheme (recursive)

浪子不回头ぞ 提交于 2019-12-06 10:46:46
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) (newline) (towers-of-hanoi(- n 1) temp source dest)))) I expected the code to work, and when I debug it I

Modified Tower of Hanoi

久未见 提交于 2019-12-05 15:38:21
We all know that the minimum number of moves required to solve the classical towers of hanoi problem is 2 n -1. Now, let us assume that some of the discs have same size. What would be the minimum number of moves to solve the problem in that case. Example, let us assume that there are three discs. In the classical problem, the minimum number of moves required would be 7. Now, let us assume that the size of disc 2 and disc 3 is same. In that case, the minimum number of moves required would be: Move disc 1 from a to b. Move disc 2 from a to c. Move disc 3 from a to c. Move disc 1 from b to c.

Code Golf: Towers of Hanoi

孤人 提交于 2019-12-04 10:08:31
问题 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. 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