题目:
问题 F: DS堆栈--迷宫求解
时间限制: 1 Sec 内存限制: 128 MB
提交: 527 解决: 333
[提交][状态][讨论版]
题目描述
给出一个N*N的迷宫矩阵示意图,从起点[0,0]出发,寻找路径到达终点[N-1, N-1]
要求使用堆栈对象来实现,具体算法参考课本3.2.4节51页
输入
第一行输入t,表示有t个迷宫
第二行输入n,表示第一个迷宫有n行n列
第三行起,输入迷宫每一行的每个方格的状态,0表示可通过,1表示不可通过
输入n行
以此类推输入下一个迷宫
输出
逐个输出迷宫的路径
如果迷宫不存在路径,则输出no path并回车
如果迷宫存在路径,将路径中每个方格的x和y坐标输出,从起点到终点,每输出四个方格就换行,最终以单词END结尾,具体格式参考示范数据
输出的代码参考如下:
if (!path.empty())
{
i=0;
while (!path1.empty())
{ cpos = path1.top();
if ( (++i)%4 == 0 )
cout<<'['<<cpos.xp<<','<<cpos.yp<<']'<<"--"<<endl;
else
cout<<'['<<cpos.xp<<','<<cpos.yp<<']'<<"--";
path1.pop();
}
cout<<"END"<<endl;
}
else
cout<<"no path"<<endl;
样例输入
2
8
0 0 0 1 1 1 1 1
1 0 0 0 1 0 0 1
1 0 0 0 1 0 0 0
1 1 0 0 0 0 0 1
0 0 1 1 0 1 1 0
0 0 0 0 0 0 1 1
1 1 1 1 1 0 0 1
0 0 0 0 1 0 0 0
7
0 0 0 1 1 1 1
1 0 0 1 0 0 1
1 0 0 1 0 0 0
1 1 0 0 0 0 1
0 0 1 1 0 1 0
1 0 0 0 0 1 0
0 0 0 0 1 1 0
样例输出
[0,0]--[0,1]--[0,2]--[1,2]--
[1,3]--[2,3]--[3,3]--[3,4]--
[4,4]--[5,4]--[5,5]--[6,5]--
[6,6]--[7,6]--[7,7]--END
no path
代码块:
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int x;
int y;
} PosType;
typedef struct{
int ord;
PosType seat;
int di;
} SElemType;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
} SqStack;
int n;
int maze[100][100];
int InitStack(SqStack *S)
{
S->base = (SElemType *)malloc(100 * sizeof(SElemType));
S->top = S->base;
S->stacksize = 100;
return 0;
}
int StackEmpty(SqStack *S)
{
if(S->top==S->base)
return 1;
else
return 0;
}
int PopStack(SqStack *S, SElemType *e)
{
S->top--;
*e = *S->top;
return 0;
}
int PushStack(SqStack *S, SElemType e)
{
*S->top = e;
S->top++;
return 0;
}
int Pass(PosType curpos)
{
if(0==maze[curpos.x][curpos.y])
return 1;
else
return 0;
}
int FootPrint(PosType curpos, int curstep)
{
maze[curpos.x][curpos.y] = curstep;
return 0;
}
PosType NextPos(PosType curpos, int di)
{
if(1==di)
curpos.y++;
else if(2==di)
curpos.x++;
else if(3==di)
curpos.y--;
else if(4==di)
curpos.x--;
return curpos;
}
int MakePrint(PosType pos)
{
maze[pos.x][pos.y] = 1;
return 0;
}
int MazePath(PosType start, PosType end)
{
int k;
SqStack S;
SElemType e;
InitStack(&S);
PosType curpos;
curpos = start;
int curstep;
curstep = 1;
do{
if(Pass(curpos))
{
FootPrint(curpos, curstep);
e.ord = curstep;
e.seat = curpos;
e.di = 1;
PushStack(&S, e);
if(curpos.x==end.x && curpos.y==end.y)
{
k = 1;
while(S.top!=S.base)
{
if(0==k%4)
{
printf("[%d,%d]--\n", S.base->seat.x-1, S.base->seat.y-1);
S.base++;
k++;
}
else
{
printf("[%d,%d]--", S.base->seat.x-1, S.base->seat.y-1);
S.base++;
k++;
}
}
printf("END\n");
return 0;
}
curpos = NextPos(curpos, 1);
curstep++;
}
else
{
if(!StackEmpty(&S))
{
PopStack(&S, &e);
while(e.di==4 && !StackEmpty(&S))
{
MakePrint(e.seat);
PopStack(&S, &e);
curstep--;
}
if(e.di<4)
{
e.di++;
PushStack(&S, e);
curpos = NextPos(e.seat, e.di);
}
}
}
} while(!StackEmpty(&S));
printf("no path\n");
return -1;
}
int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int i, j, n;
scanf("%d", &n);
for(i=0; i<100; i++)
{
for(j=0; j<100; j++)
{
maze[i][j] = 1;
}
}
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
scanf("%d", &maze[i][j]);
}
}
PosType start, end;
start.x = 1;
start.y = 1;
end.x = n;
end.y = n;
MazePath(start, end);
}
return 0;
}
题解:
- 一开始题中的栈是用结构体指针变量定义的,但是却没有用malloc申请堆空间,导致指针没初始化,控制台一直闪退。后面栈改为直接用结构体变量定义,就通过了。