import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MazePath {
public static void main(String[] args) {
int[][] maze = {{0, 1, 0, 0, 0, 0},
{0, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 0},
{0, 1, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 0}};
Position start = new Position(0, 0);
Position end = new Position(5, 5);
List<Position> pathWay = new ArrayList<Position>();
pathWay.add(start);
mazePath(maze, pathWay, end);
for(int i=0; i<mazeList.size(); i++) {
int[][] temp = mazeList.get(i);
for(int[] temp1: temp) {
System.out.println(Arrays.toString(temp1));
}
System.out.println(rightWayToEnd.get(i).size());
}
}
public static List<List<Position>> rightWayToEnd = new ArrayList<List<Position>>();
public static List<int[][]> mazeList = new ArrayList<int[][]>();
public static int lastX = 5;
public static int lastY = 5;
public static void mazePath(int[][] maze, List<Position> pathWay, Position end) {
Position lastPos = pathWay.get(pathWay.size() - 1);
//顺时针 左边相邻
if(lastPos.x+1<=lastX){
Position leftPos = new Position(lastPos.x+1, lastPos.y);
if(notInMaze(maze, leftPos) && notInPathWay(pathWay, leftPos)) {
int[][] maze1 = getNewArray(maze);
List<Position> pathWay1 = getNewList(pathWay);
pathWay1.add(leftPos);
maze1[leftPos.x][leftPos.y] = 2;
if(isEndPos(leftPos, end)) {
rightWayToEnd.add(pathWay1);
mazeList.add(maze1);
} else {
mazePath(maze1, pathWay1, end);
}
}
}
//顺时针 下边相邻
if(lastPos.y+1<=lastY){
Position bottomPos = new Position(lastPos.x, lastPos.y+1);
if(notInMaze(maze, bottomPos) &¬InPathWay(pathWay, bottomPos)) {
int[][] maze1 = getNewArray(maze);
List<Position> pathWay1 = getNewList(pathWay);
pathWay1.add(bottomPos);
maze1[bottomPos.x][bottomPos.y] = 2;
if(isEndPos(bottomPos, end)) {
rightWayToEnd.add(pathWay1);
mazeList.add(maze1);
} else {
mazePath(maze1, pathWay1, end);
}
}
}
//顺时针 右边相邻
if(lastPos.x-1>=0){
Position rightPos = new Position(lastPos.x-1, lastPos.y);
if(notInMaze(maze, rightPos) &¬InPathWay(pathWay, rightPos)) {
int[][] maze1 = getNewArray(maze);
List<Position> pathWay1 = getNewList(pathWay);
pathWay1.add(rightPos);
maze1[rightPos.x][rightPos.y] = 2;
if(isEndPos(rightPos, end)) {
rightWayToEnd.add(pathWay1);
mazeList.add(maze1);
} else {
mazePath(maze1, pathWay1, end);
}
}
}
//顺时针上边相邻
if(lastPos.y-1>=0){
Position topPos = new Position(lastPos.x, lastPos.y-1);
if(notInMaze(maze, topPos) &¬InPathWay(pathWay, topPos)) {
int[][] maze1 = getNewArray(maze);
List<Position> pathWay1 = getNewList(pathWay);
pathWay1.add(topPos);
maze1[topPos.x][topPos.y] = 2;
if(isEndPos(topPos, end)) {
rightWayToEnd.add(pathWay1);
mazeList.add(maze1);
} else {
mazePath(maze1, pathWay1, end);
}
}
}
}
private static int[][] getNewArray(int[][] maze) {
int[][] newMaze = new int[maze.length][maze[0].length];
for(int i=0; i<maze.length; i++) {
for(int j=0; j<maze[i].length; j++) {
newMaze[i][j] = maze[i][j];
}
}
return newMaze;
}
private static List<Position> getNewList(List<Position> pathWay) {
List<Position> newList = new ArrayList<Position>();
for(Position tempPos: pathWay) {
Position newPos = new Position(tempPos.x, tempPos.y);
newList.add(newPos);
}
return newList;
}
private static boolean isEndPos(Position leftPos, Position end) {
boolean flag = false;
if(end.x == leftPos.x && end.y == leftPos.y) {
flag = true;
}
return flag;
}
private static boolean notInPathWay(List<Position> pathWay, Position position) {
boolean flag = true;
for(Position temp : pathWay) {
if(temp.x == position.x && temp.y == position.y){
flag = false;
}
}
return flag;
}
private static boolean notInMaze(int[][] maze, Position position) {
boolean flag = false;
if(maze[position.x][position.y] == 0) {
flag = true;
}
return flag;
}
}
public class Position {
public int x;
public int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Position(){
}
public Position(int x, int y) {
this.x = x;
this.y = y;
}
}
标签:maze1,递归,求解,int,迷宫,public,pathWay1,Position,pathWay From: https://blog.51cto.com/u_16080829/6209386