首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >为什么我不能输出迷宫的路径?谁能告诉我?拜托

为什么我不能输出迷宫的路径?谁能告诉我?拜托
EN

Stack Overflow用户
提问于 2013-11-15 15:49:28
回答 2查看 166关注 0票数 0

关于数据结构,为什么我不能输出迷宫的路径?如果当前的迷宫地图无法通过文本文件,我使用堆栈存储当前position.Read地图的信息

将标记为"#",它是由"+“表示的wall.the方式标记。用C++写的。PS:我的英语很差。

代码语言:javascript
运行
复制
//stack.h
#ifndef _STACK_H_
#define _STACK_H_
#include<iostream>
#include<stdlib.h>

const int SIZE = 81;
using namespace std;

typedef struct
{
    int x;
    int y;
}PosType;
typedef struct SElemType{
    int ord;//steps
    PosType seat;//coordinate
    int di;//direction
}SElemType;
typedef struct SqStack{
    SElemType *base;
    SElemType *top;
    int StackSize;
}SqStack;

void Init_S(SqStack &S)
{
    S.base = new SElemType[SIZE];
    if(!S.base)
        exit(EXIT_FAILURE);
    S.top = S.base;
    S.StackSize = SIZE;
}
bool StackEmpty(SqStack S)
{
    if(S.top = S.base)
        return true;
    return false;
}
void Push(SqStack &S,SElemType e)
{
    SElemType *newbase;
    if(S.top-S.base>=S.StackSize)
    {
        newbase = new SElemType[S.StackSize*2];
        if(!S.base)
            exit(EXIT_FAILURE);
        for(int i(0);i<S.top-S.base;i++)
            *(newbase+i) = *(S.base+i);
        delete[]S.base;
        S.base = newbase;
        S.top = S.base+S.StackSize;
        S.StackSize *= 2;
    }
    *(S.top)++ = e;
}

void Pop(SqStack &S,SElemType e)
{
    if(StackEmpty(S))
        cout<<"empty stack!\n";
    else
        e = *(--S.top);
}
#endif

//maze.cpp
#include<iostream>
#include<cstdlib>
#include<fstream>
#include"stack.h"
using namespace std;
const int m = 10;
const int n = 10;
typedef char MazeG[m][n];

void Show_MG(MazeG MG)
{

    for(int i(0);i<m;i++)
    {
        for(int j(0);j<n;j++)
        {
            cout<<MG[i][j];
        }
        cout<<"\t\n";
    }
}
PosType Next(PosType &pos,int di)
{
    PosType repos;
    switch(di)
    {
    case 0://north
        repos.x = pos.x-1;
        repos.y = pos.y;
        break;
    case 1://east
        repos.x = pos.x;
        repos.y = pos.y+1;
        break;
    case 2://south
        repos.x = pos.x+1;
        repos.y = pos.y;
        break;
    case 3://west
        repos.x = pos.x;
        repos.y = pos.y-1;
        break;
    default:
        break;
    }
    return repos;
}

int MazePath(MazeG &MG,PosType begin,PosType end)
{   
    PosType curpos = begin;
    SqStack S; 
    Init_S(S);
    SElemType e;
    e.ord = 0;
    do{
        if(MG[curpos.x][curpos.y]=='*')
        {
            MG[curpos.x][curpos.y] = '+';
            e.seat = curpos;
            e.di = 0;
            e.ord++;
            Push(S,e);
            if(curpos.x==end.x&&curpos.y==end.y)
            {
                cout<<"此迷宫的一条路径如下:(+标记为路径)\n";//the path of maze:
                cout<<"走了"<<e.di<<"步到达出口\n";//The number of steps walked
                Show_MG(MG);
                return 0;
            }
            else
                curpos = Next(curpos,e.di);
        }
        else
            if(!StackEmpty(S))
            {
                Pop(S,e);
                e.ord--;
                while(e.di==3&&!StackEmpty(S))
                {
                    MG[curpos.x][curpos.y] = '#';
                    Pop(S,e);
                    e.ord--;
                }
                if(e.di<3)
                {
                    e.di++;
                    Push(S,e);
                    e.ord++;
                    curpos = Next(curpos,e.di);
                }
            }
    }while(!StackEmpty(S));
    cout<<"此迷宫没有入口到出口的路径!\n";//no path of the maze
    //return -1;
}

int main()
{
    MazeG MG;
    PosType begin,end;
    begin.x = 1; begin.y = 1;
    end.x = 8; end.y = 8;
    ifstream fin;
    fin.open("file.txt");
    if(!fin.is_open())
    {
        cout<<"erorr file!!\n";
        exit(EXIT_FAILURE);
    }
    if(fin.good())
    {
        for(int i(0);i<m;i++)
            for(int j(0);j<n;j++)
                fin>>MG[i][j];
    }
    cout<<"迷宫图为:(*代表能通过)\n";//map of maze:('*' is means through)
    Show_MG(MG);
    cout<<begin.x<<begin.y<<end.x<<end.y<<endl;
    fin.close();
    MazePath(MG,begin,end);
    return 0;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-02 11:46:25

现在,我知道我的错误的代码是我错配的数据将存储堆栈。我改变了我的代码,没错。

代码语言:javascript
运行
复制
//stack.h
SElemType Gettop(SqStack S)
{
     if(!Empty_S(S))
       return *(S.top-1);
}
//maze_path.cpp
    do{
            if(MG[curpos.x][curpos.y]=='*')
            {
                MG[curpos.x][curpos.y] = '+';
                e.seat = curpos;
                e.di = 0;
                e.ord++;
                Push(S,e);
                if(curpos.x==end.x&&curpos.y==end.y)
                {
                    cout<<"此迷宫的一条路径如下:(+标记为路径)\n";//the path of maze:
                    cout<<"走了"<<e.di<<"步到达出口\n";//The number of steps walked
                    Show_MG(MG);
                    return 0;
                }
                else
                    curpos = Next(curpos,e.di);
            }
            else
                if(!StackEmpty(S))
                {


                    if(e.di<3)
                    {
                        e.ord++;
                        curpos = Next(curpos,e.di);
                    }
                    else
                    {
                        MG[curpos.x][curpos.y] = '#';
                        Pop(S,e);
                        e = Gettop(S);
                    }
                }
        }while(!StackEmpty(S));
票数 0
EN

Stack Overflow用户

发布于 2013-11-15 16:06:26

这是不对的

代码语言:javascript
运行
复制
bool StackEmpty(SqStack S)
{
    if(S.top = S.base)
        return true;
    return false;
}

应该是这个

代码语言:javascript
运行
复制
bool StackEmpty(SqStack S)
{
    if(S.top == S.base) // == not =
        return true;
    return false;
}

使用==表示相等,而不是使用=

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20005003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档