老鼠走迷官java实现

老鼠走迷宫是递回求解的基本题型,我们在二维阵列中使用2表示迷宫墙壁,使用1来表示老鼠的行走路径,试以程式求出由入口至出口的路径。



public class MouseAlt {
static int[][] maze = {
{2,2,2,2,2,2,2},
{2,0,0,0,0,0,2},
{2,0,2,0,2,0,2},
{2,0,0,0,0,2,2},
{2,2,0,2,0,2,2},
{2,0,0,0,0,0,2},
{2,2,2,2,2,2,2}
};
/**
* @param args
*/
static int startI = 1; 
static int startJ = 1;
static int endI = 5; 
static int endJ = 5;
static int success = 0;
public static void main(String[] args) {
System.out.println("显示迷宫:\n");
for(int i = 0; i < 7; i++) {
for(int j = 0; j < 7; j++) {
if(maze[i][j] == 2) {
System.out.print("█ ");
}else {
System.out.print("* ");
}
}
System.out.println();
}

if(visit(startI, startJ) ==0) {
System.out.println("没有找到出口");
} else {
System.out.println("显示路径");
for(int i = 0; i < 7; i++) {
for(int j = 0; j < 7; j++) {
if(maze[i][j] == 2) {
System.out.print("█ ");
} else if(maze[i][j] == 1) {
System.out.print("◇ ");
}else {
System.out.print("* ");
}
}
System.out.println();
}
}
}


public static int visit(int i, int j) {
maze[i][j] = 1;
if(i == endI && j == endJ) {
success = 1;
}
if(success != 1 && maze[i][j+1] == 0) visit(i, j+1);
if(success != 1 && maze[i+1][j] == 0) visit(i+1,j);
if(success != 1 && maze[i][j-1] == 0) visit(i, j-1);
if(success != 1 && maze[i-1][j] == 0) visit(i-1,j);
if(success != 1)
maze[i][j] = 0;
return success;
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值