towers-of-hanoi

Towers of Hanoi non-recursive function

跟風遠走 提交于 2021-01-04 07:14:43
问题 I'm trying to figure out how to implement a non-recursive algorithm for the Hanoi Towers problem in function hanoi_2 below, but I have no idea how to continue... It throws an error: "can't pop from empty list" . It works somehow when I input an odd number, however, when the third turn passes things go wrong. When an even number is input as the number of discs the program doesn't even start. What is wrong? from turtle import * from tkinter import * # used for the dialog box from tkinter

Tower of Hanoi with forbidden move from source to destination (C)

烂漫一生 提交于 2020-06-29 03:57:28
问题 I am trying to write a recursive C function which would solve the tower of Hanoi , but with an extra restriction , that moving a disc from A (the source tower) to C (the destination tower) is forbidden , and vice versa. For instance , moving a single disc from A to C or C to A , would require using the auxiliary tower (B). I found a normal recursive Hanoi tower code from geeksforgeeks and checked a CS page which discussed the same problem , but I cant understand the mathematical algorithm

Hanoi Tower(Towers of Hanoi)

十年热恋 提交于 2020-01-20 06:59:08
问题 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

Haskell - parse error on input `=' [duplicate]

心不动则不痛 提交于 2020-01-17 04:32:28
问题 This question already has an answer here : Parse error in valid code [duplicate] (1 answer) Closed 5 years ago . when implementing the code for the "Towers of Hanoi" problem I get the following error message: hanoi.hs:4:24: parse error on input `=' Failed, modules loaded: none. Here is the code: hanoi 1 i j = [(i, j)] hanoi n i j = hanoi n' i otherT ++ [(i,j)] ++ hanoi n' otherT j where n' = n-1 otherT = 1+2+3-i-j Any Ideas? 回答1: Your editor and the compiler see the tabs differently. Avoid

HASKELL : Solving Towers of Hanoi

和自甴很熟 提交于 2020-01-04 02:12:12
问题 The code below solves hanoi returning a list of moves using predefined functions moveLOD,swapLOI and swapLID. MoveLOD: moves 1 disc from the first position to third the pin in the third position of the triplet. Additionally a string with information about the movement is piling on list of strings. type Pin = (Char, Int) -- Represents a rod, named for a character and the number of disks in it. type Plate = (Pin, Pin, Pin) -- Represents the configuration of the three rods.(Origin,Intermediate

Counter for Towers Of Hanoi

丶灬走出姿态 提交于 2020-01-03 18:59:16
问题 I have written a code for Towers Of Hanoi game. I don't know how to implement a counter for this program on how many times it ran. Any help will be much appreciated. public class MainClass { public static void main(String[] args) { int nDisks = 3; doTowers(nDisks, 'A', 'B', 'C'); } public static void doTowers(int topN, char from, char inter, char to) { if (topN == 1){ System.out.println("Disk 1 from " + from + " to " + to); }else { doTowers(topN - 1, from, to, inter); System.out.println("Disk

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

不打扰是莪最后的温柔 提交于 2019-12-30 09:33:10
问题 This question already has answers here : How does recursive algorithm work for Towers of Hanoi? (2 answers) Closed 5 years ago . 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,

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

断了今生、忘了曾经 提交于 2019-12-30 09:32:12
问题 This question already has answers here : How does recursive algorithm work for Towers of Hanoi? (2 answers) Closed 5 years ago . 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,

tower of hanoi - How to not skip over a peg every recursion

南楼画角 提交于 2019-12-24 22:33:36
问题 My assignment is to solve the Towers of Hanoi for any number using recursion. I wrote my code in C++. Rules: Cannot stack a larger disk on top of a smaller disk. Cannot move more than one disk at a time. ** 3. Only move a disk one peg at a time without going back to the start or going out of the end. <- This is what i'm currently struggling with. ** Following: START --> peg1 <--> peg2 <--> peg3 --> END #include <iostream> #include <time.h> using namespace std; void move(int, int, int, int,

Towers of Hanoi with “counter” in python

喜夏-厌秋 提交于 2019-12-24 01:54:21
问题 I have written a code for "Towers of Hanoi" in python and I am trying to add a counter to show how many times it has run. I tried several things like a while loop and for loops etc. but it doesn't work. I am sure that the answer is pretty easy but my brain is running on the lowest setting right now. My code looks like this: def Hanoi(n, src, dst, tmp): if n > 0: Hanoi(n - 1, src, tmp, dst) print "Move disc", chr(64 + n), "From tower", src, "to tower", dst Hanoi(n - 1, tmp, dst, src) Hanoi(4,0