import java.util.Scanner; import java.util.Scanner; import java.util.Stack; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int x = scanner.nextInt(); int y = scanner.nextInt(); int map[][] = new int[x][y]; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { map[i][j] = scanner.nextInt(); } } Stack<MazeVO> stack = new Stack(); getMaze(map, 0, 0, stack); for (MazeVO mazeVO : stack) { System.out.println("(" + mazeVO.x + "," + mazeVO.y + ")"); } } } public static boolean getMaze(int map[][], int x, int y, Stack<MazeVO> stack) { MazeVO mazeVO = new MazeVO(x, y); stack.add(mazeVO);// 放入栈 map[x][y] = 1; // 结束条件 if (x == map.length - 1 && y == map[0].length - 1) { return true; } // 向右走 if (y < map[0].length - 1 && map[x][y + 1] == 0) { if (getMaze(map, x, y + 1, stack)) { return true; } } // 向左走 if (y > 0 && map[x][y - 1] == 0) { if (getMaze(map, x, y - 1, stack)) { return true; } } // 向下走 if (x < map.length - 1 && map[x + 1][y] == 0) { if (getMaze(map, x + 1, y, stack)) { return true; } } // 向上走 if (x > 0 && map[x - 1][y] == 0) { if (getMaze(map, x - 1, y, stack)) { return true; } } stack.pop();// 如果都不满足,弹出 map[x][y] = 0; return false; } } class MazeVO { int x; int y; public MazeVO(int x, int y) { this.x = x; this.y = y; } }