java递归思想模拟解决老鼠找迷宫出路路线|代码练习

代码内容

// 递归解决老鼠找迷宫

public class Labyrinth{
	public static void main(String[] args) {
		// 先画出迷宫地图,规定0是可以走的,1是墙不能走
		int[][] map = new int[8][7];//8行7列

		for (int i = 0;i< 8; i++) {
			for (int j =0;j<7 ;j++ ) {
				map[i][j] = 0;//将所有位置都变成0
			}
		}
		// 把最上最下变成墙
		for (int i=0; i<7; i++) {
			map[0][i] = 1;
			map[7][i] = 1;
		}

		// 把最左最右变成墙
		for (int i=0; i<8; i++) {
			map[i][0] = 1;
			map[i][6] = 1;
		}

		// 制造路障

		map[3][1]=1;
		map[3][2]=1;

		// 输出原始地图
		System.out.println("\n======迷宫地图======\n");
		for (int i = 0;i< 8; i++) {
			for (int j =0;j<7 ;j++ ) {
				System.out.print(map[i][j] + " ");
			}
			System.out.println();
		}

		T t1 = new T();
		t1.findWay(map,1,1);//传入地图和初始位置为1,1

		// 输出路线地图
		System.out.println("\n======行走的地图======\n");
		for (int i = 0;i< 8; i++) {
			for (int j =0;j<7 ;j++ ) {
				System.out.print(map[i][j] + " ");
			}
			System.out.println();
		}

	}
}

class T{
	
	/*
	定规则
	1、0是可以走的,1是墙不能走,2是能走且走得通的,3是走过但是走不通
	2、目的地的位置坐标为map[6][5],当map[6][5]=2的时候表示走通了
	*/

	public boolean findWay(int[][] map,int i ,int j){//接收地图和起始位置

		if (map[6][5]==2) {//如果目标位置的值为2,就说明走通了
			return true;
		}else{

			if (map[i][j] == 0) {
				map[i][j]=2;//假设是能走通的,接着继续走,约定按照下下,右,上,左的方式去试探,这个先后不同,路线结果会不同
				if (findWay(map,i+1,j)) {//下
					return true;
				}else if (findWay(map,i,j+1)) {//右
					return true;
				}else if (findWay(map,i-1,j)) {//上
					return true;
				}else if (findWay(map,i,j-1)) {//左
					return true;
				}else{
					map[i][j]=3;//如果上下左右都走不通,说明这条路走不通,假设就不成立,标记当前为3
					return false;
				}
			}else{
				return false;//如果当前位置不为0,那就为1,2,3,其中2在上面内部也检测了,这里就为1或3,都是走不通的,返回false
			}
		
		}

	}
}

运行效果

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的老鼠迷宫最短路线Java 代码实现: ```java public class MazeSolver { private int[][] maze; private boolean[][] visited; private LinkedList<Point> path; public MazeSolver(int[][] maze) { this.maze = maze; this.visited = new boolean[maze.length][maze[0].length]; this.path = new LinkedList<>(); } public void solve() { solve(0, 0); } private boolean solve(int row, int col) { if (row < 0 || col < 0 || row >= maze.length || col >= maze[0].length) { return false; // 超出边界,返回 false } else if (visited[row][col] || maze[row][col] == 1) { return false; // 已经访问过或者是墙,返回 false } else if (row == maze.length - 1 && col == maze[0].length - 1) { path.addLast(new Point(row, col)); return true; // 到达终点,返回 true } else { visited[row][col] = true; if (solve(row + 1, col) || solve(row, col + 1) || solve(row - 1, col) || solve(row, col - 1)) { path.addLast(new Point(row, col)); return true; // 四个方向都走一遍,如果有一条路通向终点,就返回 true } else { return false; // 四个方向都走过了,都没有路通向终点,返回 false } } } public void printPath() { if (path.isEmpty()) { System.out.println("没有到路径"); } else { for (Point p : path) { System.out.println("(" + p.x + ", " + p.y + ")"); } } } public static void main(String[] args) { int[][] maze = { {0, 1, 0, 0}, {0, 0, 0, 1}, {0, 1, 0, 0}, {0, 1, 1, 0} }; MazeSolver solver = new MazeSolver(maze); solver.solve(); solver.printPath(); } } ``` 这个程序使用了递归的方法来遍历迷宫,从起点开始走,每次都尝试向四个方向走一步,如果能够到达终点就返回 true,否则返回 false。如果四个方向都走过了,都没有路通向终点,就返回 false。每次递归结束后,如果到了一条通向终点的路,就把这个点加入路径中。最后,打印路径即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值