Data structure (C language) lesson 7 - maze solution

Data structure (C language) lesson 7 - maze solution

Title Description:

The maze is represented by a rectangular matrix of m*n, and the paths and obstacles in the maze are represented by 0 and 1 respectively. A program is designed to find a path from the entrance to the exit for any maze, or to draw a conclusion that there is no path.
#include <iostream>
#include <malloc.h>
using namespace std;
#define MAXSIZE 100
typedef struct
{
    int x;
    int y;
}Coordinate;//Maze
typedef struct
{
    Coordinate position;
    int direction;
}CoordinateDir;
typedef struct
{
    CoordinateDir *top;
    CoordinateDir *base;
    int stacksize;
}SqStack;
void InitStack(SqStack &S)
{//Initialization
    S.base = new CoordinateDir[MAXSIZE];
    if(!S.base){
        cout<<"Storage allocation error"<<endl;
    }
    S.top = S.base;
    S.stacksize = MAXSIZE;
}
bool isEmpty(SqStack &S)
{
    if(S.base == S.top)
    {
        return true;
    }
    return false;
}
void Push(SqStack &S, CoordinateDir &e)
{//Insert top of stack element
    if(S.top-S.base == S.stacksize){
        cout<<"Stack full"<<endl;
    }
    *S.top++ = e;
}
void Pop(SqStack &S, CoordinateDir &e)
{//delete
    if(S.base == S.top){
        cout<<"Empty stack"<<endl;
    }
    e = *--S.top;
}
Coordinate R_position;
Coordinate C_position;
bool isPass(int **a, Coordinate &Coo)//Judge whether it is a wall
{
    if(a[Coo.x][Coo.y] == 0)
    {
        return false;
    }
    return true;
}
void signRoad(int **a, Coordinate &Coo)//Set the road to 2
{
    a[Coo.x][Coo.y] = 2;
}
void SetWall(int **a, Coordinate &Coo)//Set to wall
{
    a[Coo.x][Coo.y] = 1;
}
CoordinateDir isGo(int **a, CoordinateDir &Dir)//Judge whether it's a dead end
{
    switch(Dir.direction)
    {
        case 1: Dir.position.x++;//Go down
                if((a[Dir.position.x][Dir.position.y] != 2) && (a[Dir.position.x][Dir.position.y] != 1))
                    break;//Keep looking for
                Dir.position.x--;
        case 2: Dir.direction = 2;
                Dir.position.y++;//Turn right
                if((a[Dir.position.x][Dir.position.y] != 2) && (a[Dir.position.x][Dir.position.y] != 1))
                    break;//Keep looking for
                Dir.position.y--;
        case 3: Dir.direction = 3;
                Dir.position.x--;//Go up
                if((a[Dir.position.x][Dir.position.y] != 2) && (a[Dir.position.x][Dir.position.y] != 1))
                    break;//Keep looking for
                Dir.position.x++;
        case 4: Dir.direction = 4;
                Dir.position.y--;//Take the last direction to the left. If it is 2, it means that the point is dead, set it as a wall
                if((a[Dir.position.x][Dir.position.y] == 2) && (a[Dir.position.x][Dir.position.y] == 1))
                {
                    SetWall(a, Dir.position);//Set to wall
                    Dir.position.y++;
                }
                break;
    }
    return Dir;
}

void create_Maze(int **&a)//If no reference is passed, it should be passed to * * * a
{
    int n, m, load;
    cout<<"Please enter the length and width of Labyrinth:";cin>>n>>m;
    cout<<"Please enter maze (path is represented by 0, obstacle is represented by 1):"<<endl;
     a= new int *[50];

    //a = (int **)malloc(sizeof(int *) *n);
    for(int i = 1; i <= n; i++)
    {a[i] = new int[50];

        //a[i] = (int *)malloc(sizeof(int) *m);
        for(int j = 1; j <= m; j++)
        {
            cin>>load;
            a[i][j] = load;
        }
    }
    cout<<"Please enter the entry coordinates: "; cin>>R_position.x>>R_position.y;
    cout<<"Please enter the exit coordinates: "; cin>>C_position.x>>C_position.y;
    cout<<"Maze created successfully!"<<endl;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
}
void go_Maze(SqStack &S, int **a)
{
    CoordinateDir Dir;
    Dir.position.x = R_position.x;
    Dir.position.y = R_position.y;

    do
    {
        if(isPass(a, Dir.position) == false)//If it's not an obstacle
        {
            Dir.direction = 1;//Default seek down
            Push(S, Dir);//Put the passing points in the stack
            signRoad(a, Dir.position);//Mark this road has passed
            if(Dir.position.x == C_position.x && Dir.position.y == C_position.y)//Determine whether it is labyrinth exit
            {
                break;
            }
            Dir = isGo(a, Dir);//Looking for a dead end
        }
        else//If it's a wall
        {
            if(!isEmpty(S))
            {
                Pop(S, Dir);//Back to the previous road
                while(Dir.direction == 4 && !isEmpty(S))
                {
                    SetWall(a, Dir.position);
                    Pop(S, Dir);
                }
                if(Dir.direction < 4)
                {
                    Dir.direction++;//There are other directions on this road
                    Push(S, Dir);//This point is in the stack
                    Dir = isGo(a, Dir);
                }
            }
        }
        if(Dir.direction == 4 && isEmpty(S))
        {
            cout<<"There is no way out of the maze!"<<endl;
            break;
        }
    }while(true);
}
void showPath(int **&a, SqStack &S)
{
    SqStack s;
    InitStack(s);
    CoordinateDir Dir;
    while(!isEmpty(S))
    {
        Pop(S, Dir);
        Push(s, Dir);
    }
    while(!isEmpty(s))
    {
        Pop(s, Dir);
        cout<<"(" <<Dir.position.x<<", "<<Dir.position.y<<")";
    }
}
int main()
{
    SqStack S;
    InitStack(S);
    int **a = NULL;
    create_Maze(a);
    go_Maze(S, a);
    showPath(a, S);
    return 0;
}
/*
1 1 1 0 1
1 1 0 0 1
1 1 0 1 1
1 0 0 1 1
1 0 1 1 1

1 1 1 0 1
1 1 0 0 1
1 1 1 1 1
1 1 1 1 0
1 1 1 1 1

1 1 1 1 1 1 1
1 1 1 0 0 1 1
1 1 0 0 1 1 1
1 1 0 1 1 1 1
1 0 0 1 1 1 1
1 1 0 0 1 1 1
1 1 1 1 1 1 1


1 1 1 1 1 1 1 1 1 1
1 0 0 1 0 0 0 1 0 1
1 0 0 1 0 0 0 1 0 1
1 0 0 0 0 1 1 0 0 1
1 0 1 1 1 0 0 0 0 1
1 0 0 0 1 0 0 0 0 1
1 0 1 0 0 0 1 0 0 1
1 0 1 1 1 0 1 1 0 1
1 1 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1
*/

This blog post is only used by bloggers to record the learning process (you can comment if you have questions)

Published 8 original articles, won praise and visited 31
Private letter follow

Tags: C

Posted on Sun, 12 Jan 2020 10:04:41 -0500 by damic