递归法求解走出迷宫的所有路径。
#include <stdio.h>
#include <stdlib.h>
void visit(int, int);
int maze[9][9] = {
{2, 2, 2, 2, 2, 2, 2, 2, 2},
{2, 0, 0, 0, 0, 0, 0, 0, 2},
{2, 0, 2, 2, 0, 2, 2, 0, 2},
{2, 0, 2, 0, 0, 2, 0, 0, 2},
{2, 0, 2, 0, 2, 0, 2, 0, 2},
{2, 0, 0, 0, 0, 0, 2, 0, 2},
{2, 2, 0, 2, 2, 0, 2, 2, 2},
{2, 0, 0, 0, 0, 0, 0, 0, 2},
{2, 2, 2, 2, 2, 2, 2, 2, 2}
};
int startI = 1, startJ = 1; // enter point
int endI = 7, endJ = 7; // out point
int success = 0;
int main(void)
{
int i, j;
printf("Display map:\n");
for(i = 0; i < 9; i++) {
for(j = 0; j < 9; j++)
if(maze[i][j] == 2)
printf("██");
else
printf(" ");
printf("\n");
}
visit(startI, startJ);
printf("Find %d paths.\n",success);
return 0;
}
void visit(int i, int j)
{
int m, n;
maze[i][j] = 1;
if(i == endI && j == endJ) {
success++;
printf("\nDisplay the %dth path:\n",success);
for(m = 0; m < 9; m++) {
for(n = 0; n < 9; n++)
if(maze[m][n] == 2)
printf("██");
else if(maze[m][n] == 1)
printf("◇");
else
printf(" ");
printf("\n");
}
}
//find the path(order by RLDU)
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;
}