DS堆栈--迷宫求解

题目:

问题 F: DS堆栈--迷宫求解
时间限制: 1 Sec  内存限制: 128 MB
提交: 527  解决: 333
[提交][状态][讨论版]
题目描述
给出一个N*N的迷宫矩阵示意图,从起点[0,0]出发,寻找路径到达终点[N-1, N-1]

要求使用堆栈对象来实现,具体算法参考课本3.2.451页

输入
第一行输入t,表示有t个迷宫

第二行输入n,表示第一个迷宫有n行n列

第三行起,输入迷宫每一行的每个方格的状态,0表示可通过,1表示不可通过

输入n行

以此类推输入下一个迷宫

输出
逐个输出迷宫的路径

如果迷宫不存在路径,则输出no path并回车

如果迷宫存在路径,将路径中每个方格的x和y坐标输出,从起点到终点,每输出四个方格就换行,最终以单词END结尾,具体格式参考示范数据

输出的代码参考如下:

//path是保存路径的堆栈,堆栈中每个元素都包含x坐标和y坐标,用属性xp和yp表示

//path1是一个临时堆栈,把path的数据倒序输出到path1,使得路径按正序输出

	if (!path.empty())	//找到路径

	{	//......若干代码,实现path的数据导入path1

		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; //找不到路径输出no path

样例输入
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];//将maze和n定义为全局变量,直接设置一个大数组,但只用其中一部分

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;//用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;//经过但走不通的地方置换为墙壁1,即用1覆盖掉经过但最终不是路径栈中元素的curstep
    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);//由于有筑墙的操作,所以都要减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++)//先对所有格子置1再输入迷宫,相当于在外面筑起墙
        {
            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;
}
题解:
  1. 一开始题中的栈是用结构体指针变量定义的,但是却没有用malloc申请堆空间,导致指针没初始化,控制台一直闪退。后面栈改为直接用结构体变量定义,就通过了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值