双色,三色河内塔(Hanoi2Colors)
问题说明:
双色,三色河内塔是由河内塔演变而来的一种算法。
结果:
Java代码:
package com.allen; public class Hanoi2Colors { public static void help() { System.out.println( "Usage: java Hanoi2Colors number_of_disks"); System.out.println( "\t number_of_disks: must be a even number."); System.exit(0); }
public static void main(String[] args) { int disks = 0; try { disks = Integer.parseInt(args[0]); } catch (Exception e) { help(); } if ((disks <= 0) || (disks % 2 != 0)) { help(); } hanoi2colors(disks); }
public static void hanoi(int disks, char source, char temp, char target) { if (disks == 1) { System.out.println("move disk from " + source + " to " + target); System.out.println("move disk from " + source + " to " + target); } else { hanoi(disks-1, source, target, temp); hanoi(1, source, temp, target); hanoi(disks-1, temp, source, target); } } public static void hanoi2colors(int disks) { char source = 'A'; char temp = 'B'; char target = 'C'; for (int i = disks / 2; i > 1; i--) { hanoi(i-1, source, temp, target); System.out.println("move disk from " + source + " to " + temp); System.out.println("move disk from " + source + " to " + temp); hanoi(i-1, target, temp, source); System.out.println("move disk from " + temp + " to " + target); } System.out.println("move disk from " + source + " to " + temp); System.out.println("move disk from " + source + " to " + target); } } |
这里需要设置一下参数:
三色河内塔:
结果:
Java代码:
package com.allen; public class Hanoi3Colors { public static void help() { System.out.println( "Usage: java Hanoi3Colors number_of_disks"); System.out.println( "\tnumber_of_disks: must be a number divisible by 3."); System.exit(0); }
public static void main(String[] args) { int disks = 0; try { disks = Integer.parseInt(args[0]); } catch (Exception e) { help(); } if ((disks <= 0) || (disks % 3 != 0)) { help(); } hanoi3colors(disks); }
public static void hanoi(int disks, char source, char temp, char target) { if (disks == 1) { System.out.println("move disk from " + source + " to " + target); System.out.println("move disk from " + source + " to " + target); System.out.println("move disk from " + source + " to " + target); } else { hanoi(disks-1, source, target, temp); hanoi(1, source, temp, target); hanoi(disks-1, temp, source, target); } }
public static void hanoi3colors(int disks) { char source = 'A'; char temp = 'B'; char target = 'C'; if (disks == 3) { System.out.println("move disk from " + source + " to " + temp); System.out.println("move disk from " + source + " to " + temp); System.out.println("move disk from " + source + " to " + target); System.out.println("move disk from " + temp + " to " + target); System.out.println("move disk from " + temp + " to " + source); System.out.println("move disk from " + target + " to " + temp); } else { hanoi(disks/3-1, source, temp, target); System.out.println("move disk from " + source + " to " + temp); System.out.println("move disk from " + source + " to " + temp); System.out.println("move disk from " + source + " to " + temp); hanoi(disks/3-1, target, temp, source); System.out.println("move disk from " + temp + " to " + target); System.out.println("move disk from " + temp + " to " + target); System.out.println("move disk from " + temp + " to " + target); hanoi(disks/3-1, source, target, temp); System.out.println("move disk from " + target + " to " + source); System.out.println("move disk from " + target + " to " + source); hanoi(disks/3-1, temp, source, target); System.out.println("move disk from " + source + " to " + temp);
for (int i = disks / 3 - 1; i > 0; i--) { if (i>1) { hanoi(i-1, target, source, temp); } System.out.println("move disk from " + target + " to " + source); System.out.println("move disk from " + target + " to " + source); if (i>1) { hanoi(i-1, temp, source, target); } System.out.println("move disk from " + source + " to " + temp); } } } } |