Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //this is the start for the solve method og recsolve
- class recsolve {
- public static void main(String[] ar) {
- /*the arrays are like
- a[0] holds number of disks
- a[1] holds pole number
- */
- int disks = 5; //number of disks
- int[] a = new int[disks + 2];
- int[] c = new int[disks + 2];
- int[] b = new int[disks + 2];
- //start pole is a
- for (int i = disks + 1; i > 1; --i) {
- a[i] = disks - i + 2;//the disks at start
- b[i] = c[i] = 0;
- }
- a[0] = disks;
- b[0] = c[0] = 0;
- a[1] = 1;
- b[1] = 2;
- c[1] = 3;
- for (int i = 0; i < disks+2; i++) {
- System.out.print(a[i] + " ");
- System.out.print(b[i] + " ");
- System.out.print(c[i] + "\n");
- }
- System.out.print("\n");
- towerRec(a, b, c, disks);
- for (int i = 0; i < disks+2; i++) {
- System.out.print(a[i] + " ");
- System.out.print(b[i] + " ");
- System.out.print(c[i] + "\n");
- }
- System.out.print("\n");
- }
- public static void towerRec(int[] src, int[] tmp, int[] dst, int disks) {
- /*
- for (int i = disks + 1; i > 0; i--) {
- System.out.print(src[i] + " ");
- System.out.print(tmp[i] + " ");
- System.out.print(dst[i] + "\n\n");
- }*/
- if (disks > 0) {
- towerRec(src, dst, tmp, disks - 1);
- dst[(++dst[0]) + 1] = src[(src[0]--) + 1];
- towerRec(tmp, src, dst, disks - 1);
- System.out.print("runned \n");
- }
- //System.out.print(dst);
- // else {System.out.println("done");
- // }
- }
- }
Add Comment
Please, Sign In to add comment