老鼠走迷宫

说明:老鼠走迷宫是递回求解的基本题型,我们在二维阵列中使用2表示迷宫墙壁,使用1来表示老鼠的行走路径,试以程式求出由入口至出口的路径。
解法:老鼠的走法有上、左、下、右四个方向,在每前进一格之后就选一个方向前进,无法前进时退回选择下一个可前进方向,如此在阵列中依序测试四个方向,直到走到出口为止,这是递回的基本题,请直接看程式应就可以理解。

public class mousemaze {

         //迷宫初始化
        public static int maze[][]= new int[][]{  
        {2,2,2,2,2,2,2},
{2,0,0,0,0,0,2},
{2,0,2,2,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}};
        public static int startI=1,startJ=1;
        public static int endI=5,endJ=5;
        public static int success=0;
        public static void main(String[] args) {
            // TODO Auto-generated method stub

            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{
                        System.out.print(" ");
                    }                   
                }
                System.out.println();
            }
            //
            System.out.println("\n显示迷宫路线");
            visit(startI,startJ);

        }
        public static void visit(int i,int j){
            maze[i][j]=1;
            if(i==endI&&j==endJ){               
                    for(int a=0;a<7;a++){
                        for(int b=0;b<7;b++){
                            if(maze[a][b]==2){
                                System.out.print("█");
                            }else if(maze[a][b]==1){
                                System.out.print("*");                      
                            }else
                                System.out.print(" ");                  
                        }
                        System.out.print("\n"); 
                    }
                }       

            if(maze[i][j+1]==0)visit(i,j+1);//右`这里写代码片`
            if(maze[i+1][j]==0)visit(i+1,j);
            if(maze[i][j-1]==0)visit(i,j-1);
            if(maze[i-1][j]==0)visit(i-1,j);

            maze[i][j]=0;


        }

}
老鼠走迷宫通常是一个经典的计算机科学算法练习,可以使用递归、循环或者其他搜索策略来解决。这里提供一个简单的基于深度优先搜索(DFS)的C语言伪代码示例: ```c #include <stdio.h> #define R 5 // 迷宫行数 #define C 5 // 迷宫列数 // 定义迷宫矩阵,0表示空地,1表示墙 int maze[R][C] = { {0, 1, 1, 1, 0}, {1, 0, 0, 0, 1}, {1, 0, 1, 0, 1}, {1, 0, 0, 1, 1}, {0, 1, 1, 0, 0} // 可替换为实际迷宫矩阵 }; // 位置结构体 typedef struct Position { int row; int col; } Position; // 检查当前位置是否有效 int is_valid(Position pos) { return pos.row >= 0 && pos.row < R && pos.col >= 0 && pos.col < C && maze[pos.row][pos.col] == 0; } // 从起点开始寻找出口 void rat_in_maze(Position start) { Position current = start; // 当前位置 if (maze[current.row][current.col] == 2) { printf("Rat found the exit!\n"); return; } if (is_valid(current)) { // 搜索所有相邻的位置 Position neighbors[] = { {current.row - 1, current.col}, {current.row + 1, current.col}, {current.row, current.col - 1}, {current.row, current.col + 1} }; for (int i = 0; i < sizeof(neighbors) / sizeof(neighbors[0]); i++) { Position next = neighbors[i]; if (is_valid(next)) { maze[current.row][current.col] = 1; // 标记路径已过 rat_in_maze(next); } } maze[current.row][current.col] = 0; // 如果回溯到原来的位置,恢复为空地 } } // 主函数 int main() { Position start = {0, 0}; // 起点(例如左上角) rat_in_maze(start); return 0; } ``` 请注意,这个代码假设迷宫的出口标记为2,迷宫起始点为(0, 0),并且出口总是在迷宫的右下角。实际编写时,你需要将迷宫矩阵、起始点和出口规则替换为你需要的具体情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值