有三根杆子A, B, C. A 杆上有 N 个 ( N > 1 ) 穿孔圆盘,
盘的尺寸由下到上依次变小. 要求按下列规则将所有圆盘移至 C 杆:
每次只能移动一个圆盘; 大盘不能叠在小盘上面.
提示: 可将圆盘临时置于 B 杆, 也可将从 A 杆移出的圆盘重新移回 A 杆,
但都必须遵循上述两条规则.
问: 如何移? 最少要移动多少次 ?
二、思考
在数据结构中, 一般把 Hanoi 塔放到栈这一章. 但我觉得,
将其展开更像是二叉树, 也有点三叉的味道.
最后还不是依靠递归来完成. 最简单的就是只有一个圆盘, 这样直接可以从 A
移动到 C. 当圆盘数 N 大于 1 时(N >
1)可以把只除去最后一个的剩余圆盘作为一个整体, 使它通过 C 移动到 B ,
然后最后一个圆盘从 A 移动到 C. 这样问题就转换成了 N - 1 个圆盘从 B 经过
A 移动到 C 上.
三、具体实现
输入
三个字符代表杆, 一个整数代表需要移动的圆盘. 初始状态如下所示
1 2 3 4
paraSource 需要移动的圆盘所在杆 整个函数开始初始为 A 杆 paraIntermedium 移动圆盘用作缓存的杆 整个函数开始初始为 B 杆 paraDestination 圆盘最终需要到达的杆 整个函数开始初始为 C 杆 paraNumber 需要移动圆盘的数量 整个函数开始初始为 3
/** ********************* * Move a number of plates. * * @param paraSource The source pole. * @param paraIntermedium The intermediary pole. * @param paraDestination The destination pole. * @param paraNumber The number of plates. ********************* */ publicstaticvoidhanoi(char paraSource, char paraIntermediary, char paraDestination, int paraNumber) { if (paraNumber == 1) { System.out.println(paraSource + "->" + paraDestination + " "); return; } // Of if
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(String args[]) { hanoi('A', 'B', 'C', 3); }// Of main
/** * Huffman tree, encoding, and decoding. For simplicity, only ASCII characters * are supported. * * @author Shihuai Wen Email:wshysxcc@outlook.com */ publicclassHuffman { /** * An inner class for Huffman nodes. */ classHuffmanNode { /** * The char. Only valid for leaf nodes. */ char character;
/** * Weight. It can also be double. */ int weight;
/** * The left child. */ HuffmanNode leftChild;
/** * The right child. */ HuffmanNode rightChild;
/** * The parent. It helps constructing the Huffman code of each character. */ HuffmanNode parent;
/** ******************* * The first constructor ******************* */ publicHuffmanNode(char paraCharacter, int paraWeight, HuffmanNode paraLeftChild, HuffmanNode paraRightChild, HuffmanNode paraParent) { character = paraCharacter; weight = paraWeight; leftChild = paraLeftChild; rightChild = paraRightChild; parent = paraParent; }// Of HuffmanNode
/** ******************* * To string. ******************* */ public String toString() { StringresultString="(" + character + ", " + weight + ")";
return resultString; }// Of toString
}// Of class HuffmanNode
/** * The number of characters. 256 for ASCII. */ publicstaticfinalintNUM_CHARS=256;
/** * The input text. It is stored in a string for simplicity. */ String inputText;
/** * The length of the alphabet, also the number of leaves. */ int alphabetLength;
/** * The alphabet. */ char[] alphabet;
/** * The count of chars. The length is 2 * alphabetLength - 1 to include non-leaf * nodes. */ int[] charCounts;
/** * The mapping of chars to the indices in the alphabet. */ int[] charMapping;
/** * Codes for each char in the alphabet. It should have the same length as * alphabet. */ String[] huffmanCodes;
/** * All nodes. The last node is the root. */ HuffmanNode[] nodes;
/** ********************* * The first constructor. * * @param paraFilename The text filename. ********************* */ publicHuffman(String paraFilename) { charMapping = newint[NUM_CHARS];
readText(paraFilename); }// Of the first constructor
System.out.println("The text is:\r\n" + inputText); }// Of readText
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(String args[]) { HuffmantempHuffman=newHuffman("D:/wenshihuai/huffmantext-small.txt"); }// Of main