用户输入的迷宫位置

时间:2016-02-01 17:20:03

标签: java stack maze

我编译的程序没有错误。它在我尝试运行时终止。有人可以帮我用用户输入的开始和结束位置来运行吗?我有点失落。这是作业:

修改第4章中的迷宫问题,使其可以从用户定义的起始位置(0,0除外)开始,并搜索用户定义的终点。

代码基本上是提到的迷宫问题,只有一个Position类,没有用户输入的部分。

public class Maze {

    public static void main(String[] args) {
    }

    public interface StackADT<T> {

        public void push(T element);

        public T pop();

        public T peek();

        public boolean isEmpty();

        public int size();

        public String toString();

    }

    class LinkedStack<T> implements StackADT<T> {

        private int count;
        private LinearNode<T> top;

        public LinkedStack() {
            count = 0;
            top = null;
        }

        class LinearNode<T> {

            private LinearNode<T> next;
            private T element;

            public LinearNode() {
                next = null;
                element = null;
            }

            public LinearNode(T elem) {
                next = null;
                element = elem;
            }

            public LinearNode<T> getNext() {
                return next;
            }

            public void setNext(LinearNode<T> node) {
                next = node;
            }

            public T getElement() {
                return element;
            }

            public void setElement(T elem) {
                element = elem;
            }
        }

        class Position {

            private int x;
            private int y;

            Position() {
                x = 0;
                y = 0;
            }

            public int getx() {
                return x;
            }

            public int gety() {
                return y;
            }

            public void setx1(int a) {
                x = a;
            }

            public void sety(int a) {
                y = a;
            }

            public void setx(int x2) {

            }
        }

        private final int TRIED = 3;

        private final int PATH = 7;

        private int[][] grid = {{1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1},
        {1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1},
        {1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0},
        {0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1},
        {1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1},
        {1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1},
        {1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1},
        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};

        public StackADT<Position> push_new_pos(int x, int y,
                StackADT<Position> stack) {
            Position npos = new Position();
            npos.setx1(x);
            npos.sety(y);
            if (valid(npos.getx(), npos.gety())) {
                stack.push(npos);
            }
            return stack;
        }

        public boolean traverse() {
            boolean done = false;
            Position pos = new Position();
            Object dispose;
            StackADT<Position> stack = new LinkedStack<Position>();
            stack.push(pos);

            while (!(done)) {
                pos = stack.pop();
                grid[pos.getx()][pos.gety()] = TRIED; // this cell has been tried
                if (pos.getx() == grid.length - 1 && pos.gety() == grid[0].length - 1) {
                    done = true; // the maze is solved
                } else {
                    stack = push_new_pos(pos.getx(), pos.gety() - 1, stack);
                    stack = push_new_pos(pos.getx(), pos.gety() + 1, stack);
                    stack = push_new_pos(pos.getx() - 1, pos.gety(), stack);
                    stack = push_new_pos(pos.getx() + 1, pos.gety(), stack);
                }
            }
            return done;
        }

        private boolean valid(int row, int column) {
            boolean result = false;
            if (row >= 0 && row < grid.length
                    && column >= 0 && column < grid[row].length) {
                if (grid[row][column] == 1) {
                    result = true;
                }
            }

            return result;
        }

        public String toString() {
            String result = "\n";

            for (int row = 0; row < grid.length; row++) {
                for (int column = 0; column < grid[row].length; column++) {
                    result += grid[row][column] + "";
                }

                result += "\n";
            }
            return result;
        }

        @Override
        public void push(T element) {
            // TODO Auto-generated method stub

        }

        @Override
        public T pop() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public T peek() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public boolean isEmpty() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public int size() {
            // TODO Auto-generated method stub
            return 0;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

您在主要方法中没有做任何事情:

public static void main(String[] args){}

因此,程序无效。