迷宫样式
package com.recursion;
public class Maze {
public static void main(String[] args) {
int[][] map = new int[8][7];
for (int i = 0; i < map[0].length; i++) {
map[0][i]=1;
map[7][i]=1;
}
for (int i = 0; i < map.length; i++) {
map[i][0] = 1;
map[i][6] = 1;
}
map[3][1] = 1;
map[3][2] = 1;
//
System.out.println("=====迷宫====");
for (int[] i : map
) {
for (int y : i
) {
System.out.print(y + "\t");
}
System.out.println();
}
isRun(map,1,1);
System.out.println("====小球运动路线====");
for (int[] i : map
) {
for (int y : i
) {
System.out.print(y + "\t");
}
System.out.println();
}
}
public static boolean isRun(int[][] map,int i,int j){
if(map[6][5] == 2){
return true;
}else{
if(map[i][j] == 0){
map[i][j] = 2;
// 往上 下 左 右运动
if(isRun(map,i+1,j)){
return true;
}else if(isRun(map,i,j+1)){
return true;
}else if(isRun(map,i-1,j)){
return true;
}else if(isRun(map,i,j-1)){
return true;
}else{
map[i][j]=3;
return false;
}
}else{
return false;
}
}
}
}
运行结果
数字1代表围墙,数字2代表小球走过的路线
标签:map,return,递归,int,isRun,小球,System,数据结构,out From: https://www.cnblogs.com/xming2023/p/17094066.html