已确定迷宫求解所有路线(递归)

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) &&notInPathWay(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) &&notInPathWay(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) &&notInPathWay(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;
	}
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
问题描述: 以一个m*n的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。设计一个程序,对任意设定的迷宫出从入口(0,0)到出口(m-1,n-1)的通路通路总数,或得出没有通路的结论。例如下图, 0(入口) 1 0 1 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0(出口) 从入口到出口有6条不同的通路。 而下图: 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 1 0 1 0 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 0 0 0 从入口到出口则没有通路。 算法设计: 给定一个m*n的长方阵表示迷宫,设计算法输出入口到出口的通路通路总数,或得出没有通路的结论。 算法提示: 和皇后问题与分书问题类似。可以用二维数组存储迷宫数据,对于迷宫中任一位置,均可约定有东、南、西、北四个方向可通。从当前位置a(用(x,y)表示一个位置,假定它是以向右的x轴和向下的y轴组成的平面上的一个点)出发依次尝试四个方向是否有路,若某个方向的位置b可通,则按照同样的方法继续从b出发寻找。若到达出口,则找到一条通路。 数据输入: 由文件input.txt 提供输入数据。第一行是m和n的值,空格分隔,其后共m行。每行有n个数字,数和数之间用空格分隔。 结果输出: 将计算出的所有从入口到出口的通路输出到文件output.txt 中。若没有通路,则将0写入文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值