【发布时间】:2018-03-09 09:38:50
【问题描述】:
我已经实现了一个使用递归解决Sierpinski carpet 问题的解决方案。现在我想使用堆栈而不是递归方法来解决谢尔宾斯基地毯。我正在尝试将我的递归方法转换到堆栈中,但是当我从递归方法中推送变量时遇到了麻烦。这是我必须推送和弹出的一段代码
drawGasket(x + i * sub, y + j * sub, sub);
当您调用 drawGasket(0, 0, 729) 时,您应该会在屏幕上看到以下内容:
递归方法:
public void drawGasket(int x, int y, int side) {
int sub = side / 3;
//Draw center square
g2d.fill(new Rectangle2D.Double(x + sub, y + sub, sub - 1, sub - 1));
if(sub >= 3) {
//Draw 8 surrounding squares
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if (j!=1 || i != 1)
drawGasket(x + i * sub, y + j * sub, sub);
}
}
}
}
堆栈实现:
public void stack (int x, int y, int side ){
GenericStack<Integer> s = new GenericStack<>();
int sub = side /3;
g2d.fill(new Rectangle2D.Double(x + sub, y + sub, sub - 1, sub - 1));
while (!s.isEmpty()){
x=s.pop();
if (sub >=3){
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if (j!=1 || i != 1){
int operation = x+i*sub;
s.push(operation);
int operation2 = y+j*sub;
s.push(operation2);
s.push(sub);
}
}
}
}
}
}
我的堆栈类:
public class GenericStack<T> {
private int size; // size
private Node<T> head; // node head
public GenericStack() { // constructor
head = null; // head is null
size = 0; // size is zero
}
public void push(T element) {
if(head == null) { // if head is null
head = new Node(element); // head is node
} else {
Node<T> newNode = new Node(element);
newNode.next = head;
head = newNode;
}
size++;
}
public T pop() {
if(head == null)
return null;
else {
T topData = head.data;
head = head.next;
size--;
return topData;
}
}
public T top() {
if(head != null)
return head.data;
else
return null;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
private class Node<T> {
private T data;
private Node<T> next;
public Node(T data) {
this.data = data;
}
}
【问题讨论】:
-
我没有看到问题。
-
问题(据我了解)是“如何编写我已经编写的递归解决方案的迭代(基于堆栈)版本?”