int maze [7][10] = { /*迷宫数组*/
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 1, 0, 1, 0, 0, 0, 0, 1,
1, 0, 1, 0, 1, 0, 1, 1, 0, 1,
1, 0, 1, 0, 1, 1, 1, 0, 0, 1,
1, 0, 1, 0, 0, 0, 0, 0, 1, 1,
1, 0, 0, 0, 1, 1, 1, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
#include <stdio.h>
int maze [7][10] = { /*迷宫数组*/
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 1, 0, 1, 0, 0, 0, 0, 1,
1, 0, 1, 0, 1, 0, 1, 1, 0, 1,
1, 0, 1, 0, 1, 1, 1, 0, 0, 1,
1, 0, 1, 0, 0, 0, 0, 0, 1, 1,
1, 0, 0, 0, 1, 1, 1, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
int find_path (int x, int y)
{
if (x == 1 && y == 1) { /*是否是迷宫出口*/
maze[x][y] = 2; /*记录最后走过的路径*/
return (1);
} else if (maze[x][y] == 0) { /*是不是可以走*/
maze[x][y] = 2; /*记录已走过的路径*/
if (( find_path (x - 1, y) + /*递归往上*/
find_path (x + 1, y) + /*递归往下*/
find_path (x, y - 1) + /*递归往左*/
find_path (x, y + 1)) > 0) /*递归往右*/
return (1);
else {
maze[x][y] = 0; /*此路不通,取消记号*/
return (0);
}
} else return (0);
}
int main ()
{
int i, j;
find_path (5, 8);
printf ("迷宫的路径如下图所示:\n");
for (i = 1; i < 6; i++) {
for (j = 1; j < 9; j++)
printf ("%d", maze[i][j]); /*输出各坐标*/
printf ("\n");
}
return (0);
}
beyes@linux-beyes:~/C/structer/recursion> ./maze.exe
迷宫的路径如下图所示:
21010000
21010110
21011100
21222221
22211122
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |