/************************************************************************/
/* 数据结构:栈应用:求解迷宫问题 */
/* 挑灯看剑-shuchangs@126.com 2010-10 */
/* 云歌国际(Cloud Singers International) www.cocoral.com */
/************************************************************************/
//上接版面 《数据结构:栈应用:求解迷宫问题2》
void printMzePath(Stack S, int (*mtx)[9], int m, int n)
{
Status StackLocate(Stack S, int x, int y, NodePointer rtnNode);
COUNT i, j;
Node N =
{
NULL, NULL, NULL
};
printf("矩阵信息:%d行,%d列!\n", m, n);
if (!StackIsEmpty(S))
{
for (i = 0; i < m; i++)
{
printf("row=%d ", i);
for (j = 0; j < n; j++)
{
if (StackLocate(S, i, j, &N))
printf(" %c ", '*');
else
printf(" %d ", *(*(mtx + i) + j));
}
printf("\n");
}
}
else
{
for (i = 0; i < m; i++)
{
printf("row=%d ", i);
for (j = 0; j < n; j++)
{
printf(" %d ", *(*(mtx + i) + j));
}
printf("\n");
}
}
}
Status StackLocate(Stack S, int x, int y, NodePointer rtnNode)
{
COUNT i = 0;
COUNT n = S.len;
NodePointer p = S.base;
Status status = ERROR;
for (i = 0; i < n; i++)
{
if (p->data.x == x && p->data.y == y)
{
rtnNode = p;
status = OK;
break;
}
else
{
p = p->next;
}
}
if (p == NULL)
{
status = ERROR;
}
return status;
}
运行测试结果如下:
矩阵信息:9行,9列!
row=0 0 0 0 0 0 0 0 0 0
row=1 0 * 1 0 1 1 0 1 0
row=2 0 1 1 0 1 1 0 1 0
row=3 0 1 1 1 1 0 1 1 0
row=4 0 1 0 0 0 1 1 1 0
row=5 0 1 1 0 0 1 0 1 0
row=6 0 1 0 1 1 1 0 1 0
row=7 0 1 1 1 1 0 0 1 0
row=8 0 0 0 0 0 0 0 0 0
迷宫求解结束!
栈长度:21
打印结点信息(栈底到栈顶):
[1] foot[1][1] = 1
[2] foot[1][1] = 1
[3] foot[1][2] = 1
[4] foot[2][2] = 1
[5] foot[3][2] = 1
[6] foot[3][1] = 1
[7] foot[4][1] = 1
[8] foot[5][1] = 1
[9] foot[6][1] = 1
[10] foot[7][1] = 1
[11] foot[7][2] = 1
[12] foot[7][3] = 1
[13] foot[7][4] = 1
[14] foot[6][4] = 1
[15] foot[6][5] = 1
[16] foot[5][5] = 1
[17] foot[4][5] = 1
[18] foot[4][6] = 1
[19] foot[4][7] = 1
[20] foot[5][7] = 1
[21] foot[6][7] = 1
矩阵信息:9行,9列!
row=0 0 0 0 0 0 0 0 0 0
row=1 0 * * 0 1 1 0 1 0
row=2 0 1 * 0 1 1 0 1 0
row=3 0 * * 1 1 0 1 1 0
row=4 0 * 0 0 0 * * * 0
row=5 0 * 1 0 0 * 0 * 0
row=6 0 * 0 1 * * 0 * 0
row=7 0 * * * * 0 0 1 0
row=8 0 0 0 0 0 0 0 0 0
Press any key to continue