Data structure - stacks and queues and Applications - Maze

Data structure - stacks and queues and Applications - Maze

【Purpose】

To enable students to understand the characteristics stacks and queues in order to utilize them in the context of practical problems, and will also consolidate and master basic operations of these two methods of construction structure.

[Experimental] content

1. Description of the problem: the long square represents a maze of m × n, 1 and 0, respectively, represent the maze of paths and obstacles. Design a program to arbitrarily set maze, obtains a path from the inlet to the outlet, passage, or no conclusion.
2, the basic requirements:
First, implemented as a linked list structure of a stack type memory, then write a non-recursive procedure for solving the maze. Obtained via output triples (i, j, d), wherein: (i, j) indicates a coordinate maze, d represents the direction come next coordinate. Such as: a labyrinth for the following data, an output path is: (1,1,1), (1,2,2), (2,2,2), (3,2,3), (3,1 , 2) ...
3, the test data: maze test data are as follows: bottom left (1,1) for the inlet, bottom right (8,9) for export.
0 0. 1 0 0 0. 1 0
0 0. 1 0 0 0. 1 0
0 0 0 0. 1. 1 0. 1
0. 1. 1. 1 0 0. 1 0
0 0 0. 1 0 0 0 0
0. 1 0 0 0. 1 0. 1
0. 1 . 1. 1. 1. 1 0 0
. 1. 1 0 0 0 0. 1. 1
. 1. 1 0 0 0 0 0 0

4, to achieve prompt: computer solving a maze usually use the word "exhaustive solving" approach, starting from the entry, to explore along a certain direction, if we go through, then move on; otherwise, return along the same route, for a direction continue to explore until the exit position, seek a path. If all possible paths to explore all failed to reach the exit, then there is no set path maze.
Two-dimensional array can store data labyrinth is normally set at the entry point labeled (1,1), the exit point of the subscript (n, n). To handle your convenience, you can add a circle around the maze of obstacles. A labyrinth for any position, may have agreed to the east, south, west, north four directions can pass.

##【Experimental data】

#include <stdio.h>
#include <math.h>
#define M 8
#define N 8
#define MaxSize 100

typedef int ElemType;

int a[M+2][N+2]=
{
    {1,1,1,1,1,1,1,1,1,1},
    {1,0,1,1,0,0,0,1,0,1},
    {1,0,1,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,0,1,0,0,0,0,0,2,1},
    {1,1,1,1,1,1,1,1,1,1}
};

typedef struct
{
    int i;              //当前方块的行号
    int j;              //当前方块的列号
    int d;             //di是下一可走相邻方位的方位号     1为向右走  2为向下走   3为向左走  4为向上走
} Box;

typedef struct
{
    Box base[MaxSize];
    int top;            //栈顶指针
} SqList;

void MazePath(SqList S)
{
    int q=0,m,n,k=0;  //q 为计数器
    m=1;
    n=1;
    S.top=-1;
    do
    {
        k++;
        if(a[m][n]==0)
        {
            a[m][n]=1;
            S.top++;
            S.base[S.top].i=m;
            S.base[S.top].j=n;
            S.base[S.top].d=1;
            n=n+1;
            q=q+1;
            continue;
        }
        if(a[m][n]==1)
        {
            if(S.base[S.top].d==1)
            {
                n=n-1;
                S.base[S.top].d=2;
                m=m+1;
                continue;
            }
            if(S.base[S.top].d==2)
            {
                m=m-1;
                S.base[S.top].d=3;
                n=n-1;
                continue;
            }
            if(S.base[S.top].d==3)
            {
                n=n+1;
                S.base[S.top].d=4;
                continue;
            }
            if(S.base[S.top].d==4)
            {
                S.top--;
                q--;
                continue;
            }
        }
        if(a[m][n]==2)
        {
            printf(" 步数为:%d\n",q);
            break;
        }
    }
    while(S.top>-1);
    printf("执行了%d次循环\n",k);
}

int main()
{
    void MazePath(SqList S);
    SqList S;
    MazePath(S);
    return 0;
}


[Results]

Here Insert Picture Description

Published 27 original articles · won praise 4 · Views 1255

Guess you like

Origin blog.csdn.net/WX_timi/article/details/104207558