70,026
社区成员




void NextPos(unsigned *x, unsigned *y, short di) /* 定位下一个位置 */
{
switch (di) {
case 1:++(*x); break;
case 2:++(*y); break;
case 3:--(*x); break;
case 4:--(*y); break;
default:
break;
}
}
2、加上“}”后的代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
unsigned ord, x, y;/*通道块在路径上的序号和在迷宫中的坐标位置*/
short di; /* 从此通道块走向下一通道块的“方向” */
}Elemtype;
typedef struct Node { /* 定义单链表 */
Elemtype data;
struct Node* next;
} Node, *LinkList;
typedef struct { /* 定义链栈结构 */
LinkList top; /* 栈顶指针 */
unsigned length; /* 栈中元素个数 */
} LStack;
void DestroyStack(LStack *S) /* 销毁栈 */
{
LinkList q;
while (S->top) {
q = S->top;
S->top = S->top->next; /* 修改栈顶指针 */
free(q);/* 释放被删除的结点空间 */
}
S->length = 0;
}
void InitStack(LStack *S) /* 构造空栈 */
{
S->top = NULL; /* 栈顶指针初值为空 */
S->length = 0; /* 空栈中元素个数为 0 */
}
int Pop(LStack *s, Elemtype *e)
{ /* 若栈不空,则删除 S 的栈顶元素,用 e 返回栈顶元素的值。*/
LinkList q;
if (!s->top){
return 0;
}
*e = s->top->data; /* 返回栈顶元素 */
q = s->top;
s->top = s->top->next; /* 修改栈顶指针 */
--s->length;/* 栈的长度减1 */
free(q);/* 释放被删除的结点空间 */
return 1;
}
int Push(LStack *S, Elemtype e)
{ /* 在栈顶之上插入元素 e 为新的栈顶元素 */
LinkList p = (LinkList)malloc(sizeof *p); /* 建新的结点 */
if (!p) {
return 0; /* 存储分配失败 */
}
p->data = e;
p->next = S->top; /* 链接到原来的栈顶 */
S->top = p; /* 移动栈顶指针 */
++S->length; /* 栈的长度增1 */
return 1;
}
void NextPos(unsigned *x, unsigned *y, short di); /* 定位下一个位置 */
int main(void)
{
LStack S, T;
unsigned x, y, curstep, i = 0;/* 迷宫坐标,探索步数 */
Elemtype e;
char maze[10][10] = {
{ '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', ' ', ' ', '#', ' ', ' ', ' ', '#', ' ', '#' },
{ '#', ' ', ' ', '#', ' ', ' ', ' ', '#', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', '#', '#', ' ', ' ', '#' },
{ '#', ' ', '#', '#', '#', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', '#', ' ', ' ', ' ', '#', ' ', ' ', '#' },
{ '#', ' ', '#', '#', '#', '#', '#', '#', ' ', '#' },
{ '#', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', ' ', '#' },
};
InitStack(&S);
InitStack(&T);
printf("迷宫图形,#号代表墙壁,空格代表通路:\n");
for (x = 0; x < 10; x++) {
for (y = 0; y < 10; y++)
printf("%c", maze[x][y]);
printf("\n");
}
x = 1; /*迷宫起点*/
y = 0;
curstep = 1; /* 探索第一步 */
do { /* 进入迷宫 */
if (maze[y][x] == ' ') { /* 如果当前位置可以通过 */
maze[y][x] = '1';/* 留下足迹 */
e.x = x;
e.y = y;
e.di = 1;
e.ord = curstep;
if (!Push(&S, e)) { /* 加入路径,即压栈 */
fprintf(stderr, "内存不足。\n");
}
if (x == 8 && y == 9) { /* 到达终点 */
break;
}
NextPos(&x, &y, 1); /* 下一位置是当前位置的东邻 */
curstep++;
}
else { /* 如果当前位置不能通过 */
if (S.length != 0) {
Pop(&S, &e);
while (e.di == 4 && S.length != 0) {
maze[e.y][e.x] = '0'; /* 留下不能通过足迹 */
Pop(&S, &e); /* 退回一步,即出栈 */
}
if (e.di < 4) {
e.di++;
if (!Push(&S, e)) { /* 加入路径,即压栈 */
fprintf(stderr, "内存不足。\n");
}
x = e.x; /* 重置坐标 */
y = e.y;
NextPos(&x, &y, e.di); /* 寻找下一位置 */
}
}
}
} while (S.length != 0);
printf("走出迷宫路线,1代表走过的路,0代表试探过的路径\n");
for (x = 0; x < 10; x++) {
for (y = 0; y < 10; y++) {
printf("%c", maze[x][y]);
if (maze[x][y] == '1')
i++;
}
printf("\n");
for (x = 0; x < i; x++)
{
Pop(&S, &e);
Push(&T, e);
}
printf("出迷宫顺序,(X坐标,Y坐标,前进方向):\n");
while (T.length != 0)
{
printf("(%d,%d,%d)\t", T.top->data.x, T.top->data.y, T.top->data.di);
Pop(&T, &e);
}
DestroyStack(&S);
getchar();
return 0;
}
}
void NextPos(unsigned *x, unsigned *y, short di) /* 定位下一个位置 */
{
switch (di) {
case 1:++(*x); break;
case 2:++(*y); break;
case 3:--(*x); break;
case 4:--(*y); break;
default:
break;
}
}
int main( void )
{
LStack S,T;
unsigned x,y,curstep,i=0;/* 迷宫坐标,探索步数 */
Elemtype e;
char maze[10][10] = {
{'#',' ','#','#','#','#','#','#','#','#'},
{'#',' ',' ','#',' ',' ',' ','#',' ','#'},
{'#',' ',' ','#',' ',' ',' ','#',' ','#'},
{'#',' ',' ',' ',' ','#','#',' ',' ','#'},
{'#',' ','#','#','#',' ',' ',' ',' ','#'},
{'#',' ',' ',' ','#',' ',' ',' ',' ','#'},
{'#',' ','#',' ',' ',' ','#',' ',' ','#'},
{'#',' ','#','#','#','#','#','#',' ','#'},
{'#','#',' ',' ',' ',' ',' ',' ',' ','#'},
{'#','#','#','#','#','#','#','#',' ','#'},
};
InitStack(&S);
InitStack(&T);
printf("迷宫图形,#号代表墙壁,空格代表通路:\n");
for ( x = 0;x < 10;x++) {
for ( y = 0;y < 10;y++ )
printf("%c", maze[x][y]);
printf("\n");
}
x = 1; /*迷宫起点*/
y = 0;
curstep = 1; /* 探索第一步 */
do { /* 进入迷宫 */
if ( maze[y][x] == ' ' ) { /* 如果当前位置可以通过 */
maze[y][x] = '1';/* 留下足迹 */
e.x = x;
e.y = y;
e.di = 1;
e.ord = curstep;
if (!Push(&S,e) ) { /* 加入路径,即压栈 */
fprintf( stderr,"内存不足。\n" );
}
if ( x == 8 && y == 9 ) { /* 到达终点 */
break;
}
NextPos(&x, &y, 1); /* 下一位置是当前位置的东邻 */
curstep++;
}
else { /* 如果当前位置不能通过 */
if (S.length!=0) {
Pop(&S,&e);
while (e.di == 4 && S.length!=0) {
maze[e.y][e.x] = '0'; /* 留下不能通过足迹 */
Pop(&S, &e); /* 退回一步,即出栈 */
}
if (e.di < 4) {
e.di++;
if (!Push(&S,e) ) { /* 加入路径,即压栈 */
fprintf( stderr,"内存不足。\n" );
}
x = e.x; /* 重置坐标 */
y = e.y;
NextPos(&x,&y,e.di); /* 寻找下一位置 */
}
}
}
} while (S.length!=0);
printf("走出迷宫路线,1代表走过的路,0代表试探过的路径\n");
for ( x = 0;x < 10;x++ ) {
for ( y = 0;y < 10;y++ ) {
printf("%c",maze[x][y]);
if(maze[x][y]=='1')
i++;
}
printf("\n");
}
for(x=0;x<i;x++)
{ Pop(&S,&e);
Push(&T,e);
}
printf("出迷宫顺序,(X坐标,Y坐标,前进方向):\n");
while(T.length!=0) {
printf("(%d,%d,%d)\t",T.top->data.x,T.top->data.y,T.top->data.di);
Pop(&T,&e);
}
DestroyStack(&S);
getchar();
return 0;
}
main函数里缺少一个右大括号。如果没有理解错的话,是这个循环的右大括号少了。
for ( x = 0;x < 10;x++ ) {
for ( y = 0;y < 10;y++ ) {
printf("%c",maze[x][y]);
if(maze[x][y]=='1')
i++;
}
printf("\n");
}
在printf("\n");后面少了一个}