package com.java.study; class MazeClass{ private int []H = {0 ,1 ,0, -1}; private int []V = {-1 ,0 ,1, 0};//上,右,下,左 private char[][]Maze; private int len; public MazeClass(char[][]Maze, int len){ this.Maze = Maze; this.len = len; } public void FindPath(int x, int y){ if(x==len || y==len){ for(int i = 0 ; i < len ;i++){ for(int j = 0 ; j < len; j++){ char fenge = j < len-1 ? ' ':'\n'; System.out.print(Maze[i][j]); System.out.print(fenge); } } }else for(int k = 0 ; k < 4; k++){ if(x>=0 && y>=0 && y<len && x<len && 'o'==Maze[x][y]){ Maze[x][y] = ' '; FindPath(x+V[k] , y+H[k]);//按四个方向去搜 Maze[x][y] = 'o'; } } } } public class Maze { public static void main(String[] args) { char Maze[][] = {{'X','X','X','X','X','X','X','X'}, {'o','o','o','o','o','X','X','X'}, {'X','o','X','X','o','o','o','X'}, {'X','o','X','X','o','X','X','o'}, {'X','o','X','X','X','X','X','X'}, {'X','o','X','X','o','o','o','X'}, {'X','o','o','o','o','X','o','o'}, {'X','X','X','X','X','X','X','X'}}; MazeClass mc = new MazeClass(Maze,Maze.length); mc.FindPath(1, 0); } }