Pome资料库
Pome 切换暗/亮/自动模式 切换暗/亮/自动模式 切换暗/亮/自动模式 返回首页

老鼠走迷宫(一)

说明

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

解法

老鼠的走法有上、左、下、右四个方向,在每前进一格之后就选一个方向前进,无法前进时退回选择下一个可前进方向,如此在阵列中依序测试四个方向,直到走到出口为止,这是递迴的基本题,请直接看程式应就可以理解。

演算法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
Procedure GO(maze[]) [
    VISIT(maze, STARTI, STARTJ);
]
    
Procedure VISIT(maze[], i, j) [
    maze[i][j] = 1; 

    IF(i == ENDI AND j == ENDJ) 
        success = TRUE; 

    IF(success != TRUE AND maze[i][j+1] == 0)
        VISIT(maze, i, j+1); 
    IF(success != TRUE AND maze[i+1][j] == 0)
        VISIT(maze, i+1, j); 
    IF(success != TRUE AND maze[i][j-1] == 0)
        VISIT(maze, i, j-1); 
    if(success != TRUE AND maze[i-1][j] == 0)
        VISIT(maze, i-1, j); 

    IF(success != TRUE) 
        maze[i][j] = 0; 
]

编码参考

C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <stdlib.h> 

int visit(int, int); 

int maze[7][7] = {{2, 2, 2, 2, 2, 2, 2}, 
                  {2, 0, 0, 0, 0, 0, 2}, 
                  {2, 0, 2, 0, 2, 0, 2}, 
                  {2, 0, 0, 2, 0, 2, 2}, 
                  {2, 2, 0, 2, 0, 2, 2}, 
                  {2, 0, 0, 0, 0, 0, 2}, 
                  {2, 2, 2, 2, 2, 2, 2}}; 

int startI = 1, startJ = 1;  // 入口
int endI = 5, endJ = 5;  // 出口
int success = 0;

int main(void) { 
    int i, j; 

    printf("显示迷宫:\n"); 
    for(i = 0; i < 7; i++) { 
        for(j = 0; j < 7; j++) 
            if(maze[i][j] == 2) 
                printf("█"); 
            else 
                printf("  "); 
        printf("\n"); 
    } 

    if(visit(startI, startJ) == 0)
        printf("\n没有找到出口!\n"); 
    else { 
        printf("\n显示路径:\n"); 
        for(i = 0; i < 7; i++) { 
            for(j = 0; j < 7; j++) { 
                if(maze[i][j] == 2) 
                    printf("█"); 
                else if(maze[i][j] == 1) 
                    printf("◇"); 
                else 
                    printf("  "); 
            } 
            printf("\n"); 
        } 
    } 

    return 0; 
} 

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; 
}

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
public class Mouse {
    private int startI, startJ;  // 入口 
    private int endI, endJ;  // 出口
    private boolean success = false;
    
    public static void main(String[] args) {
        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, 2, 0, 2, 2}, 
                        {2, 2, 0, 2, 0, 2, 2}, 
                        {2, 0, 0, 0, 0, 0, 2}, 
                        {2, 2, 2, 2, 2, 2, 2}};
        
        System.out.println("显示迷宫:"); 
        for(int i = 0; i < maze.length; i++) { 
            for(int j = 0; j < maze[0].length; j++) 
                if(maze[i][j] == 2) 
                    System.out.print("█"); 
                else 
                    System.out.print("  "); 
            System.out.println(); 
        }

        Mouse mouse = new Mouse();
        mouse.setStart(1, 1);
        mouse.setEnd(5, 5);
        
        if(!mouse.go(maze)) {
            System.out.println("\n没有找到出口!");
        }
        else {
            System.out.println("\n找到出口!");
            for(int i = 0; i < maze.length; i++) { 
                for(int j = 0; j < maze[0].length; 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 void setStart(int i, int j) {
        this.startI = i;
        this.startJ = j;
    }
    
    public void setEnd(int i, int j) {
        this.endI = i;
        this.endJ = j;
    }
    
    public boolean go(int[][] maze) {
        return visit(maze, startI, startJ);
    }
    
    private boolean visit(int[][] maze, int i, int j) {
        maze[i][j] = 1; 

        if(i == endI && j == endJ) 
            success = true; 

        if(!success && maze[i][j+1] == 0) 
            visit(maze, i, j+1); 
        if(!success && maze[i+1][j] == 0)
            visit(maze, i+1, j); 
        if(!success && maze[i][j-1] == 0)
            visit(maze, i, j-1); 
        if(!success && maze[i-1][j] == 0)
            visit(maze, i-1, j); 

        if(!success) 
            maze[i][j] = 0; 
        
        return success; 
    }
}