610 likes | 705 Views
Hanoi executing. 0. 1. Hanoi(2,a,c,b). Hanoi(3,a,b,c). move (a,3,c). Hanoi(2,b,a,c). 0. 1. Hanoi(2,a,c,b). Hanoi(3,a,b,c). move (a,3,c). Hanoi(2,b,a,c). 2. Hanoi(1,a,b,c). move(a,2,b). Hanoi(1,c,a,b). 0. 1. 2. Hanoi(1,a,b,c). Hanoi(2,a,c,b). move(a,2,b). Hanoi(1,c,a,b).
E N D
0 1 Hanoi(2,a,c,b) Hanoi(3,a,b,c) move (a,3,c) Hanoi(2,b,a,c)
0 1 Hanoi(2,a,c,b) Hanoi(3,a,b,c) move (a,3,c) Hanoi(2,b,a,c) 2 Hanoi(1,a,b,c) move(a,2,b) Hanoi(1,c,a,b)
0 1 2 Hanoi(1,a,b,c) Hanoi(2,a,c,b) move(a,2,b) Hanoi(1,c,a,b) Hanoi(3,a,b,c) move (a,3,c) Hanoi(2,b,a,c) 3 move(a,1,c) move(c,1,b)
0 1 2 3 Hanoi(1,a,b,c) move(a,1,c) Hanoi(2,a,c,b) move(a,2,b) Hanoi(1,c,a,b) move(c,1,b) Hanoi(3,a,b,c) move (a,3,c) Hanoi(2,b,a,c) Hanoi(1,b,c,a) move(b,2,c) Hanoi(1,a,b,c)
0 1 2 3 Hanoi(1,a,b,c) move(a,1,c) Hanoi(2,a,c,b) move(a,2,b) Hanoi(1,c,a,b) move(c,1,b) Hanoi(3,a,b,c) move (a,3,c) Hanoi(1,b,c,a) move(b,1,a) Hanoi(2,b,a,c) move(b,2,c) move(a,1,c) Hanoi(1,a,b,c)
Hanoi(3,a,b,c) Hanoi(1,a,b,c) move(a,2,b) Hanoi(2,a,c,b) Hanoi(1,c,a,b)
Hanoi(1,b,c,a) a c b 2 1 3 move (a,3,c) move(b,2,c) Hanoi(2,b,a,c) Hanoi(1,c,a,b)
0 tier void main (void) { int n; unsigned char a,b,c; n=3; a=1; b=2; c=3; hanoi(n, a, b, c); 0: . . . }
0 tier void main (void) { int n; unsigned char a,b,c; n=3; a=1; b=2; c=3; hanoi(n, a, b, c); 0: . . . }
0 tier void main (void) { int n; unsigned char a,b,c; n=3; a=1; b=2; c=3; hanoi(n, a, b, c); 0: . . . }
0 tier void main (void) { int n; unsigned char a,b,c; n=3; a=1; b=2; c=3; hanoi(n, a, b, c); 0: . . . }
0 tier void main (void) { int n; unsigned char a,b,c; n=3; a=1; b=2; c=3; hanoi(n, a, b, c); 0: } 0, 3, a, b, c
1 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 3, a, b, c 0, 3, a, b, c
1 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 3, a, b, c 0, 3, a, b, c
1 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 3, a, b, c 0, 3, a, b, c
1 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 3, a, b, c 2, a, c, b 0, 3, a, b, c
1 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 3, a, b, c 2, a, c, b 6, 2, a, c, b 0, 3, a, b, c
2 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 2, a, c, b 6, 2, a, c, b 0, 3, a, b, c
2 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 2, a, c, b 6, 2, a, c, b 0, 3, a, b, c
2 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 2, a, c, b 6, 2, a, c, b 0, 3, a, b, c
2 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 2, a, c, b 1, a, b, c 6, 2, a, c, b 0, 3, a, b, c
2 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 2, a, c, b 1, a, b, c 6, 1, a, b, c 6, 2, a, c, b 0, 3, a, b, c
3 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 1, a, b, c 6, 1, a, b, c 6, 2, a, c, b 0, 3, a, b, c
3 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 1, a, b, c 6, 1, a, b, c 6, 2, a, c, b 0, 3, a, b, c
3 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 1, a, b, c a, 1, c 6, 1, a, b, c 6, 2, a, c, b 0, 3, a, b, c
3 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 1, a, b, c 6, 1, a, b, c 6, 2, a, c, b 0, 3, a, b, c
2 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 2, a, c, b a, 2, b 6, 2, a, c, b 0, 3, a, b, c
2 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 2, a, c, b 6, 2, a, c, b 1,c,a,b 0, 3, a, b, c
2 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 2, a, c, b 8, 1, c, a, b 6, 2, a, c, b 1,c,a,b 0, 3, a, b, c
3 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 1, c, a, b 8, 1, c, a, b 6, 2, a, c, b 0, 3, a, b, c
3 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 1, c, a, b 8, 1, c, a, b 6, 2, a, c, b 0, 3, a, b, c
3 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 1, c, a, b c, 1, b 8, 1, c, a, b 6, 2, a, c, b 0, 3, a, b, c
3 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 1, c, a, b 8, 1, c, a, b 6, 2, a, c, b 0, 3, a, b, c
2 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 2, a, c, b 6, 2, a, c, b 0, 3, a, b, c
2 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 2, a, c, b 6, 2, a, c, b 0, 3, a, b, c
1 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 3, a, b, c a, 3, c 0, 3, a, b, c
1 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 3, a, b, c 2, b, a, c 0, 3, a, b, c
1 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 3, a, b, c 8, 2, b, a, c 2, b, a, c 0, 3, a, b, c
2 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 2, b, a, c 8, 2, b, a, c 0, 3, a, b, c
2 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 2, b, a, c 8, 2, b, a, c 0, 3, a, b, c
2 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 2, b, a, c 8, 2, b, a, c 0, 3, a, b, c
2 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 2, b, a, c 1, b, c, a 8, 2, b, a, c 0, 3, a, b, c
2 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 2, b, a, c 1, b, c, a 6, 1, b, c, a 8, 2, b, a, c 0, 3, a, b, c
3 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 1, b, c, a 6, 1, b, c, a 8, 2, b, a, c 0, 3, a, b, c
3 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 1, b, c, a 6, 1, b, c, a 8, 2, b, a, c 0, 3, a, b, c
3 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 1, b, c, a b, 1, a 6, 1, b, c, a 8, 2, b, a, c 0, 3, a, b, c
3 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 1, b, c, a 6, 1, b, c, a 8, 2, b, a, c 0, 3, a, b, c
2 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 2, b, a, c b,2,c 8, 2, b, a, c 0, 3, a, b, c
2 tier • void hanoi(int n, char x, char y, char z) • 1 { • 2 if(n==1) • 3 move(x,1,z); • 4 else{ • 5 hanoi(n-1,x,z,y); • move(x,n,z); • hanoi(n-1,y,x,z); • 8 } • 9 } 2, b, a, c 8, 2, b, a, c 1,a,b,c 0, 3, a, b, c