說明
老鼠走迷宮是遞迴求解的基本題型,我們在二維陣列中使用2表示迷宮牆壁,使用1來表示老鼠的行走路徑,試以程式求出由入口至出口的路徑。
解法
老鼠的走法有上、左、下、右四個方向,在每前進一格之後就選一個方向前進,無法前進時退回選擇下一個可前進方向,如此在陣列中依序測試四個方向,直到走到出口為止,這是遞迴的基本題,請直接看程式應就可以理解。
演算法
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;
]
實作
#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;
}
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;
}
}