recursive hanoi tower code with condition that we cant move disk from peg 1 to 3

so we have to change this recursive hanoit tower code and add this condition to it :
we cant move any disk from peg 0 to peg 2 directly
also we can't move any disk from peg 2 to peg 0 directly , meaning we should move it to the peg 1 then to peg 0
i can't think of any code that does this

anyone have any idea ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
using namespace std;

/*
  Hanoi is the capital of Vietnam. It is also a logical puzzle game that can be solved very effectively
  with recursion. The setup of Hanoi is simple. We have three pegs. On each of these pegs is a series
  of disks decreasing in size from the bottom of the peg to the top of the peg. The goal is to move
  all these disks to another peg following these rules:
    -Only one disk can be moved at a time.
    -Only the topmost disk of any given peg can be moved to another peg.
    -A larger disk cannot be placed ontop a smaller disk.

  The simplest way of doing this is via Divide and Conquer.
  The code is simple, but keeping track of what is going on may cause your brain to have a stack-overflow.

  Params:
    diskSize refers to the maximum number of disks we wish to move. If we have 5 disks and we wish
    to move all five disks to another peg then this value should be 5.
    5 = largest disk and 0 = smallest disk.

    source is the peg where the disks currently exist.

    dest is the peg we want the disks to be moved to.

    spare is the peg that temporally stores pegs. This is necessary in order for us to shuffle disks
    around without violating the rules.
*/
void hanoi(int diskSize, int source, int dest, int spare)
{
  //This is our standard termination case. We get to here when we are operating on the
  //smallest possible disk.
  if(diskSize == 0)
	{
		std::cout << "Move disk " << diskSize << " from peg " << source << " to peg " << dest << endl;
	}
	else
	{
		//Move all disks smaller than this one over to the spare.
    //So if diskSize is 5, we move 4 disks to the spare. This leaves us with 1 disk
    //on the source peg.
    //Note the placement of the params.
    //We are now using the dest peg as the spare peg. This causes each recursion to ping-pong
    //the spare and dest pegs.
		hanoi(diskSize - 1, source, spare, dest);

		//Move the remaining disk to the destination peg.
		std::cout << "Move disk "  << diskSize << " from peg " << source << " to peg " << dest << endl;

		//Move the disks we just moved to the spare back over to the dest peg.
		hanoi(diskSize - 1, spare, dest, source);
	}
}

int main()
{
  //Move all 3 disks from peg 0 to peg 1 using peg 2 as a temporary.
  hanoi(2, 0, 1, 2);
  return 0;
}
Last edited on
See: 'Tower of Hanoi with adjacency requirement (Ex. 8.1.18)' in
http://appsrv.cse.cuhk.edu.hk/~chi/csc2110-2009/notes/T10.pdf
thanks!!
that was helpful i read the algorithm but i can't still think of any c++ code that does that

it should be recursive aswell , how should i change the program above to do that ?
anyone ?! help?!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>

int num_moves_non_adjacent( int num_disks )
{
    // http://appsrv.cse.cuhk.edu.hk/~chi/csc2110-2009/notes/T10.pdf
    if( num_disks == 1 ) return 2 ;
    else return 3 * num_moves_non_adjacent( num_disks-1 ) + 2 ;
}

// invariant: towers are numbered 1, 2, 3 see line 23: 'may be commented out if...'
// bool is_adjacent( int tower_a, int tower_b ) { return tower_a == 2 || tower_b == 2 ; }

int move_number = 0 ;

void move_one( int disc, int source, int dest )
{
    std::cout << ++move_number << ". move disc #" << disc << " from peg " << source
              << " to peg " << dest << '\n' ;
}

void hanoi( int diskSize, int source = 1, int dest = 3, int spare = 2 )
{
    /*////////// may be commented out if it is guaranteed that /////////////////////////
    //////////// initially, source and dest are not adjacent; because then, ///////////////
    //////////// none of the recursive calls will ever be for adjacent pegs ///////////////
    if( is_adjacent( source, dest ) )
    {
        if( diskSize == 0 ) move_one( 0, source, dest ) ;
        else
        {
            hanoi( diskSize-1, source, spare, dest ) ;
            move_one( diskSize, source, dest ) ;
            hanoi( diskSize-1, spare, dest, source ) ;
        }
    }
    else
    *//////////////////////////////////////////////////////////////////////////////////////////////
    {
        if( diskSize == 0 )
        {
            move_one( 0, source, spare ) ;
            move_one( 0, spare, dest ) ;
        }
        else
        {
            hanoi( diskSize-1, source, dest, spare ) ;
            move_one( diskSize, source, spare ) ;
            hanoi( diskSize-1, dest, source, spare ) ;
            move_one( diskSize, spare, dest ) ;
            hanoi( diskSize-1, source, dest, spare ) ;
        }
    }
}

http://coliru.stacked-crooked.com/a/9892272649ab958e
THANKS!!!!!!!!
Topic archived. No new replies allowed.