Divide and Conquer Algorithm
The basic steps divide and conquer algorithm
@author:qyx 2013/6/3
Partition method has three steps on each recursion level
1) Decomposition: the original problem into several smaller, independent of each other, the same as the original problem in the form of sub-problems
2) Solution: If a smaller sub-problems to be solved rather easily scale the direct solution, or solutions of each sub-problems recursively
3) merger: the merger of each sub-problem solution for the original problem solution
Tower of Hanoi algorithm uses divide and conquer the problem:
Solving process can be divided into three steps:
If there is a disk 1 A-> C
2 If the plate number more than two, we always can be seen as two disks, one lowermost plate, top plate 2
1) the uppermost first disc A-> B
2) The lowermost disc A-> C
3) all the discs from column B B-> C
package com.qyx.dac; public class Hanoitower { public static void main(String[] args) { hanoitower(5, 'A', 'B', 'C'); } // Tower of Hanoi moving method // use the divide and conquer algorithm public static void hanoitower ( int NUM, char A, char B, char C) { // If only one disk IF (NUM == 1 ) { System.out.println ( "first disk from the" A + + "->" + C); } The else { // if the number of discs is greater than 2, then I can be seen two discs: a lowermost disc above the disc 2 // 1 uppermost first disc A-> B, during the mobile use to C hanoitower (. 1-NUM , a, C, B); // 2 the most bottom disc A-> C System.out.println ( "first" + num + "disks from" + a + "->" + C); // . 3 B all discs from the column using B-> C, the process moves to a column hanoitower (-NUM. 1 , B, a, C); } } }