首页 > 其他分享 >已确定迷宫求解所有路线(递归)

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

时间:2023-04-20 12:07:08浏览次数:31  
标签:maze1 递归 求解 int 迷宫 public pathWay1 Position pathWay


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

相关文章

  • sql with语句查询 递归查询
    with语句查询可以将一个子查询作为一个结果,相当于一个i临时表多次使用WITHt1AS(SELECT1ASid,'bird'AScname),t2AS(SELECT1ASid,'123'ASinfo)SELECTt1.cname,t2.infoFROMt1,t2WHEREt1.id=t2.id;t1和t2两个临时结果,后续查询可以使用。最后的查询也可以再......
  • 递归-leetcode 114
    给你二叉树的根结点 root ,请你将它展开为一个单链表:展开后的单链表应该同样使用 TreeNode ,其中 right 子指针指向链表中下一个结点,而左子指针始终为 null 。展开后的单链表应该与二叉树 先序遍历 顺序相同。示例1:输入:root=[1,2,5,3,4,null,6]输出:[1,null,2,null,3,nu......
  • 组织树查询-Jvava实现(递归)
    1.首先查询出组织机构就是一个简单的查询List<Dept>deptList=mapper.getDeptList();Map<Long,OrgNode>nodeMap=newHashMap<>();List<Long>rootIds=newArrayList<>();for(Deptdept:deptList){Longd......
  • 带约束条件的运筹规划问题求解(模拟退火算法实现)
    0.写在前面超级简单的模拟退火算法实现ε٩(๑>₃<)۶з搭配最简单的线性规划模型进行讲解!但是如果需要的话可以直接修改编程非线性问题哦(´つヮ⊂︎)1.模型描述及处理1.1线性规划模型\[max\,f(x)=10x_1+9x_2\]\(s.t.\)\[6x_1+5x_2\leq{60}\tag{1}\]\[10x_1+20x_2\leq{......
  • 4月18日leetcode二叉树几种遍历方式的非递归和递归
    给你二叉树的根节点 root ,返回它节点值的 前序 遍历。 示例1:二叉树的前序中序和后序遍历算法是学习二叉树必不可少的,若是使用c语言遍历前中后序还是比较繁琐的,因为要考虑遍历结果存放的序列大小问题,想要解决这个问题就得想用递归计算二叉树的节点数量,再调用递归子函数完......
  • 【VRP问题】基于混合遗传算法求解车辆路径规划问题附matlab代码
    ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。......
  • 实际问题中用到的算法——递归算法确定插帧顺序
    问题:现在需要给一个视频序列插帧,插帧算法要求每次只能由两帧输入插值得到其中间帧。如果现在需要给一个视频做4倍(或者更高的8,16倍等类似)的插帧,则一个插帧的思路是当前视频每相邻帧之间插入3帧,即:假设插帧前视频帧序号是0,4,8,12…,则插帧时补充相邻帧跨过的3个序号,得到插......
  • 二叉树前序遍历,中序遍历,后序遍历的统一模板写法【递归和非递归】
    二叉树有三种深度遍历的方式,分别是前序,中序和后序,分别对应LeetCode的144,94,145三道题目。三种遍历方式的递归写法都差不多,也比较容易,相信大家都已经烂熟于心了。但是非递归写法,目前还有很多不同的写法,比如循环条件,有的用栈是否为空,有的用指针是否指向NULL。这样比较混乱的形式,不利于......
  • java 递归方法 计算1-100之间的所有自然数的和 计算1-100之间所
    packageprectice;/***递归方法的使用**递归方法的定义:一个方法体内调用他自身**①方法递归包含了一种隐式循环,它会重复执行某段代码,但这种重发执行无须循环控制。*②递归一定要向已知方向递归,否则这种递归就变成了无穷递归,类似死循环。** 例1:计......
  • 2023-04-17 算法面试中常见的树和递归问题
    二叉树和递归0LeetCode297二叉树的序列化和反序列化序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。请设计一个算法来实现二叉树的序列化与......