1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| public class Main{ public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int startx = in.nextInt(); int starty = in.nextInt(); int endx = in.nextInt(); int endy = in.nextInt(); int[][] maze = new int[n+1][m+1]; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ maze[i][j] = in.nextInt(); } } maze[startx][starty] = 2; dfs(startx,starty,endx,endy,maze); } private static void dfs(int startx, int starty, int endx, int endy, int[][] maze) { if(startx==endx&&starty==endy){ printf(maze); return; } if(startx-1 >= 1 && maze[startx-1][starty]==0){ maze[startx-1][starty]=2; dfs(startx-1,starty,endx,endy,maze); maze[startx-1][starty] = 0; } if(startx+1 <= maze.length-1 && maze[startx+1][starty]==0){ maze[startx+1][starty]=2; dfs(startx+1,starty,endx,endy,maze); maze[startx+1][starty]=0; } if(starty-1 >= 1 && maze[startx][starty-1]==0){ maze[startx][starty-1]=2; dfs(startx,starty-1,endx,endy,maze); maze[startx][starty-1]=0; } if(starty+1 <= maze[startx].length-1 && maze[startx][starty+1]==0){ maze[startx][starty+1]=2; dfs(startx,starty+1,endx,endy,maze); maze[startx][starty+1]=0; } }
private static void printf(int[][] arr){ for(int i = 0; i < arr.length; i++){ for(int j = 0; j < arr[i].length; j++){ System.out.print(arr[i][j]+" "); } System.out.println(""); } } }
|