Java Maze Solver - 调用pop()时不会弹出堆栈

时间:2018-05-20 04:00:12

标签: java stack backtracking maze pop

我正在完成一项任务,我必须通过回溯来解决迷宫(使用堆栈!)并且代码的逻辑基本完成,但主要的问题是每当我在我的上调用pop()堆栈,它没有弹出。所以我现在手动添加(硬编码)迷宫里的部分应该pop()。我正在使用我自己的堆栈,它使用链接节点并运行JUnit和Main测试,它确实有效(现在在这里怀疑自己)。我也使用了Java堆栈,得到了相同的结果。

这是我的代码逻辑:正如你在方法mazeSolver中看到的那样,在底部,我有一些if语句检查我是否(执行的操作)是否在某一点,它将"回溯&# 34;,但我手动设置位置。最后一个else语句是我pop()的部分。非常感谢任何帮助。

public class MazeSolver {

private char[][] printMaze;
private char[][] solveMaze;

public MazeSolver() {
    super();

}

private class Position {
    private int x;
    private int y;

    Position(int y, int x) {

        this.x = x;
        this.y = y;
    }

    public int getX() {
        return this.x;
    }

    public int getY() {
        return this.y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }
}

public boolean solve(boolean printUpdates) {

    char space = ' ';
     Stack<Position> stack = new Stack<Position>();
    //Stack stack = new Stack();
    Position cp = new Position(1, 0);
    boolean done = true;
    char c = 'C';
    char x = 'X';
    int i = 0;
    while (done) {

        // check right

        if (printMaze[cp.getY()][cp.getX() + 1] == space && printMaze[cp.getY()][cp.getX() + 1] != x) {
            cp.setX(cp.getX() + 1);
            stack.push(cp);
            printMaze[cp.getY()][cp.getX()] = 'C';

        }

        // check bottom
        else if (printMaze[cp.getY() + 1][cp.getX()] == space && printMaze[cp.getY() + 1][cp.getX()] != x
                && printMaze[cp.getY() + 1][cp.getX()] != x) {
            cp.setY(cp.getY() + 1);
            stack.push(cp);
            printMaze[cp.getY()][cp.getX()] = 'C';

        }

        // check top
        else if (printMaze[cp.getY() - 1][cp.getX()] == space && printMaze[cp.getY() - 1][cp.getX()] != x) {
            cp.setY(cp.getY() - 1);
            stack.push(cp);
            printMaze[cp.getY()][cp.getX()] = 'C';
        }
        // check left
        else if (printMaze[cp.getY()][cp.getX() - 1] == space && printMaze[cp.getY()][cp.getX() - 1] != x) {
            cp.setX(cp.getX() - 1);
            stack.push(cp);
            printMaze[cp.getY()][cp.getX()] = 'C';

        }

        //else {
/*
            if (i == 6) {
                printMaze[cp.getY()][cp.getX()] = 'X';
                cp.setY(1);
                cp.setX(5);
            } else if (i == 27) {
                printMaze[cp.getY()][cp.getX()] = 'X';
                cp.setY(1);
                cp.setX(20);
            }

            else if (i == 37) {
                printMaze[cp.getY()][cp.getX()] = 'X';
                cp.setY(2);
                cp.setX(18);
            }

            else if (i == 68) {
                printMaze[cp.getY()][cp.getX()] = 'X';
                cp.setY(13);
                cp.setX(22);

            } else if (i == 69) {
                printMaze[cp.getY()][cp.getX()] = 'X';
                cp.setY(14);
                cp.setX(22);
            }

            else if (i == 70) {
                printMaze[cp.getY()][cp.getX()] = 'X';
                cp.setY(15);
                cp.setX(22);
            } else if (i == 71) {
                printMaze[cp.getY()][cp.getX()] = 'X';
                cp.setY(16);
                cp.setX(22);
            } else if (i == 72) {
                printMaze[cp.getY()][cp.getX()] = 'X';
                cp.setY(17);
                cp.setX(22);
            } else if (i == 103) {
                printMaze[cp.getY()][cp.getX()] = 'X';
                cp.setY(21);
                cp.setX(31);
            } */else {
                printMaze[cp.getY()][cp.getX()] = 'X';
                stack.pop();
                cp.setY(((Position) stack.top()).getY());
                cp.setX(((Position) stack.top()).getX());
            }

        //}

        i++;

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // System.out.println(stack.top().getX() + ", " + stack.top().getY());
        System.out.println(cp.getY() + ", " + cp.getX() + " i = " + i);

        printMaze();

    }

    System.out.println("success");
    return false;
}

public void printMaze() {

    for (int i = 0; i < printMaze.length; i++) {
        for (int j = 0; j < printMaze[i].length; j++) {
            System.out.print(printMaze[i][j]);
        }
        System.out.println("");
    }
}

public boolean loadMaze(String filename) {
    BufferedReader br = null;
    FileReader fr = null;
    ArrayList<String> lines = new ArrayList<String>();
    try {

        fr = new FileReader(filename);
        br = new BufferedReader(fr);
        String line;
        br = new BufferedReader(new FileReader(filename));

        while ((line = br.readLine()) != null) {
            lines.add(line);
        }
        printMaze = new char[lines.size()][];
        solveMaze = new char[lines.size()][];
        for (int i = 0; i < lines.size(); i++) {
            printMaze[i] = new char[lines.get(i).length()];
            solveMaze[i] = new char[lines.get(i).length()];

            for (int j = 0; j < lines.get(i).length(); j++) {
                solveMaze[i][j] = lines.get(i).charAt(j);
                printMaze[i][j] = lines.get(i).charAt(j);
                if (solveMaze[i][j] == 'S') {
                    // hint you need to do this but you do not have the
                    // instance variable yet
                    // start = new Position(i, j);

                }

            }
        }

    } catch (IOException e) {

        e.printStackTrace();

    } finally {

        try {

            if (br != null)
                br.close();

            if (fr != null)
                fr.close();

        } catch (IOException ex) {

            return false;

        }

    }

    return true;
}

如果感兴趣,这是我的堆栈实现。

public class Stack<Item> implements StackInterface<Item> {

private int size;

private class Link {
    private Item data;
    public Link next;

    public Link(Item data, Link next) {
        this.data = data;
        this.next = next;
    }

    public Item getData() {
        return data;
    }
}

private Link topStackLink = null;


public Stack() {
    this.size = 0;
}


@Override
public void push(Item item) {

    if (topStackLink == null) {
        topStackLink = new Link(item, null);

    } else {
        topStackLink = new Link(item, topStackLink);
    }
    this.size++;

}


@Override
public void pop() {
    // TODO Auto-generated method stub
    if (topStackLink != null) {
        topStackLink = topStackLink.next;
        this.size--;
    } else {
        throw new java.util.EmptyStackException();
    }

}

@Override
public Item top() {

    if (topStackLink != null) {
        return topStackLink.data;

    } else {
        throw new java.util.EmptyStackException();
    }
}


@Override
public Item topAndPop() {
    // TODO Auto-generated method stub

    if (topStackLink != null) {
        Item item = topStackLink.data;
        pop();
        return item;
    } else {
        throw new java.util.EmptyStackException();

    }

}


@Override
public boolean isEmpty() {

    if (topStackLink == null) {
        return true;
    } else {
        return false;
    }
}


@Override
public void makeEmpty() {
    // TODO Auto-generated method stub

    topStackLink = null;
    this.size = 0;

}


@Override
public int size() {

    return this.size;
}

0 个答案:

没有答案