/* algo3-9.c 用递归函数求解迷宫问题(求出所有解) */
#include"c1.h" /* 根据《PASCAL程序设计》(郑启华编著)中的程序改编 */
#include"func3-1.c" /* 定义墙元素值为0,可通过路径为-1,通过路径为足迹 */
void Try(PosType cur,int curstep)
{ /* 由当前位置cur、当前步骤curstep试探下一点 */
int i;
PosType next; /* 下一个位置 */
PosType direc[4]={{0,1},{1,0},{0,-1},{-1,0}}; /* {行增量,列增量},移动方向,依次为东南西北 */
for(i=0;i<=3;i++) /* 依次试探东南西北四个方向 */
{
next.x=cur.x+direc[i].x; /* 根据移动方向,给下一位置赋值 */
next.y=cur.y+direc[i].y;
if(m[next.x][next.y]==-1) /* 下一个位置是通路 */
{
m[next.x][next.y]=++curstep; /* 将下一个位置设为足迹 */
if(next.x!=end.x||next.y!=end.y) /* 没到终点 */
Try(next,curstep); /* 由下一个位置继续试探(降阶递归调用,离终点更近) */
else /* 到终点 */
{
Print(); /* 输出结果(出口,不再递归调用) */
printf("\n");
}
m[next.x][next.y]=-1; /* 恢复为通路,以便在另一个方向试探另一条路 */
curstep--; /* 足迹也减1 */
}
}
}
int main()
{
Init(-1); /* 初始化迷宫,通道值为-1 */
printf("此迷宫从入口到出口的路径如下:\n");
m[begin.x][begin.y]=1; /* 入口的足迹为1 */
Try(begin,1); /* 由第1步入口试探起 */
}
运行:
[root@localhost algorithm]# ls
algo3-9.c c1.h func3-1.c
[root@localhost algorithm]# gcc algo3-9.c -o algo3-9
在包含自 algo3-9.c:2 的文件中:
c1.h:8:28: 错误:io.h:没有那个文件或目录
c1.h:10:34: 错误:process.h:没有那个文件或目录
algo3-9.c:37: 错误:程序中有游离的 ‘\32’
algo3-9.c:37:2: 警告:文件未以空白行结束
[root@localhost algorithm]# vim c1.h
[root@localhost algorithm]# gcc algo3-9.c -o algo3-9
algo3-9.c:37: 错误:程序中有游离的 ‘\32’
algo3-9.c:37:2: 警告:文件未以空白行结束
[root@localhost algorithm]# vim algo3-9.c
[root@localhost algorithm]# gcc algo3-9.c -o algo3-9
[root@localhost algorithm]# ls
algo3-9 algo3-9.c c1.h func3-1.c
[root@localhost algorithm]# ./algo3-9
�������Թ�������,����(������ǽ)��5,5
�������Թ���ǽ��Ԫ�
�����������Թ���ǽÿ����Ԫ������,���
2,2
�Թ��ṹ����:
0 0 0 0 0
0 -1 -1 -1 0
0 -1 0 -1 0
0 -1 -1 -1 0
0 0 0 0 0
���������ڵ�����,������1,1
���������ڵ�����,������3,3
���Թ������ڵ����ڵ�·������:
0 0 0 0 0
0 1 2 3 0
0 -1 0 4 0
0 -1 -1 5 0
0 0 0 0 0
0 0 0 0 0
0 1 -1 -1 0
0 2 0 -1 0
0 3 4 5 0
0 0 0 0 0
[root@localhost algorithm]#