原文:
In the classic problem of the Towers of Hanoi, you have 3 rods and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from top to bottom (e.g., each disk sits on top of an even larger one). You have the following constraints:
- Only one disk can be moved at a time.
- A disk is slid off the top of one rod onto the next rod.
- A disk can only be placed on top of a larger disk.
Write a program to move the disks from the first rod to the last using Stacks
译文:
编程解决汉诺塔问题,使用数据结构栈(偷个懒,如果不知道汉诺塔是什么,请自行Google)
使用递归方法如下:
package chapter_3_StacksandQueues;
import java.util.Scanner;
/**
* @author LiangGe
* 汉诺塔,递归方法
*
*/
public class Question_3_4 {
public static void shift(char start, char end, int n) {
System.out.println("移动 " + n + " , 从 " + start + " 至此 " + end);
}
public static void change(char start, char end, char mid, int n) {
if(n == 1) {
shift(start, end, n);
return;
}
change(start, mid, end, n-1);
shift(start, end, n);
change(mid, end, start, n-1);
}
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
scanner.nextLine();
while(num -- > 0) {
int n = scanner.nextInt();
change('A', 'C' , 'B', n);
}
}
}
使用非递归方法如下:
package chapter_3_StacksandQueues;
import java.util.Scanner;
class Node_4 {
public Node_4 pre;
public char start;
public char end;
public char mid;
public int m;
public int n;
public Node_4(int m ,int n, char start, char end, char mid) {
this.m = m;
this.n = n;
this.start = start;
this.end = end;
this.mid = mid;
}
}
class Stack_4 {
private int size;
private Node_4 top;
public Stack_4() {
size = 0;
top = null;
}
public boolean isEmpty() {
return top == null;
}
public void push(Node_4 node) {
if(top == null) {
top = node;
top.pre = null;
} else {
node.pre = top;
top = node;
}
size ++;
}
public void pop() {
if(top == null) {
System.out.println("Stack is empty!");
return;
}
top = top.pre;
size --;
}
public Node_4 top() {
if(top == null) {
System.out.println("Stack is empty!");
return null;
}
return top;
}
}
/**
* @author LiangGe
* 汉诺塔,使用栈实现
*/
public class Question_3_4_2 {
public static void change(char start, char end, char mid, int n) {
Stack_4 stack = new Stack_4();
// 记录当前三根柱子的状态
Node_4 node = new Node_4(1, n, start, end, mid);
stack.push(node);
while(!stack.isEmpty()) {
Node_4 curNode = stack.top();
stack.pop();
if(curNode.m != curNode.n) {
stack.push(new Node_4(curNode.m, curNode.n-1, curNode.mid, curNode.end, curNode.start));
stack.push(new Node_4(curNode.n, curNode.n, curNode.start, curNode.end, curNode.mid));
stack.push(new Node_4(curNode.m, curNode.n-1, curNode.start, curNode.mid, curNode.end));
} else {
System.out.format("move %d %c --> %c \n", curNode.m, curNode.start, curNode.end);
}
}
}
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
scanner.nextLine();
while(num -- > 0) {
int n = scanner.nextInt();
// 汉诺塔,n个盘子从A移动到C,借助于B
change('A', 'C' , 'B', n);
}
}
}
参考自:http://www.hawstein.com/posts/3.4.html