Sunday, March 14, 2010

Dan Doctor In Arizona



Recursive Algorithm Pseudocode

hanoi (n, source, destination, auxiliary)

if n = 1



then move a disk from the tower to the tower source destination / / {} solution of the recursive method

if

/ / {n-1 disks to move the tower to the tower auxiliary source}
call hanoi (n-1, source, auxiliary target) / / recursive call} {
Move a disc from the source tower to the destination tower

/ / {n-1 disks to move the auxiliary tower to tower

destination} method call hanoi (n-1, axuliar, destination, origin) / / recursive call} {



order order







Iterative Algorithm Pseudocode
/ / the arguments origin, destination and axuliar are variables representing the three towers of the original problem (may be structures or simply the name of each tower, in this case represent only the name of the towers is therefore assumed to be in text format).
/ / n is the number of disks
/ / Pilane, battery, and battery are Pilad type structures
BATTERY / / stop is a variable of type integer.
/ / varaux is a variable of type text (in this case because the towers are represented only by name, ie, this variable must be the same type as origin, destination and auxiliary)
/ / flag is a boolean type variable

iterative solution (n, source, destination, auxiliary)

to stop = 0 and flag = false
repeat while (n> 0) and (flag = false) repeat

while (N> 1)

/ / are stored in the battery current values \u200b\u200bof the parameters
stop = stop +1
Pilan [top] = n
stack [top] = origin
Pilad [top] = stack desthio
[top] = auxiliary

n = n-1
varaux = destination = destination
auxiliary aid = varaux


End / / end repeat while (N> 1)

move disk from source to destination
flag = true

if top> 0 then / / means that the batteries are empty

n = Pilan [top]
origin = stack [top]
target = Pilad [top]
auxiliary = stack [top] top = top-
1

move disk from source to destination

n = n-1
varaux = source source =

auxiliary aid = varaux


flag = false end / / end if top> 0 then
End / / end repeat while (n> 0) and (band = false)
End / / end method

0 comments:

Post a Comment