# Include <stdio. h>
# Include <iostream> # include <stdlib. h> # include <time. h> # define stack_init_size 1000 # define stackincrement 10 # define OK 1 # define overflow 0 # define error 0 // typedef int selemtype; typedef struct selemtype {int X; int y ;}; typedef struct {selemtype * base; selemtype * Top; int stacksize;} sqstack; int initstack (sqstack & S) {S. base = (selemtype *) malloc (stack_init_size * sizeof (selemtype); If (! S. base) Exit (overflow); S. top = S. base; S. stacksize = stack_init_size; Return OK;} void push (sqstack & S, selemtype & E) {If (S. top-S.base> = S. stacksize) {S. base = (selemtype *) realloc (S. base, (S. stacksize + stackincrement) * sizeof (selemtype); If (! S. base) Exit (overflow); S. top = S. base + S. stacksize; S. stacksize + = stackincrement;} * s. top ++ = E;} selemtype POP (sqstack & S, selemtype & E) {// If (S. top = S. base) Return Error; E = * -- S. top; Return e;} selemtype gettop (sqstack S, selemtype & E) {e = * (S. top-1); Return e;} int stackempty (sqstack & S) {// if Stack S is empty, OK is returned; otherwise, errorif (S. base = S. top) Return OK; elsereturn error;} int main () {int m; sqstack s; initstack (s); printf ("Enter the size of the maze Small (M * m) "); scanf (" % d ", & M ); // The array format indicates eight direction int move [8] [2] ={{}, {1}, {1,-1 }, {0,-1}, {-1,-1}, {-1, 0}, {-1, 1 }}; // use struct to indicate the position struct position {int x, y ;}; // used to record and output the relevant symbols in the Maze exploration path, including 1. char maze [20] [20]; // stack is used to store int I, X, Y, OK; selemtype P; // The 0th rows of the Two-dimensional array, the m + 1 rows, The 0th columns, and the M + 1 columns are all elements // set to "1", indicating the border of the maze; the elements in column 1st of row 1st and column M of Row M are set to "0", indicating the entrance and exit of the maze. Other element values are generated using a random // function. Srand (time (0); For (x = 1; x <= m; X ++) for (y = 1; y <= m; y ++) maze [x] [Y] = 48 + rand () % 2; maze [1] [1] = '0'; maze [m] [m] = '0 '; for (x = 0; x <= m + 1; X ++) {maze [x] [0] = '1 '; maze [x] [M + 1] = '1';} For (y = 0; y <= m + 1; y ++) {maze [0] [Y] = '1'; maze [M + 1] [Y] = '1';} p. X = 1; p. y = 1; push (S, P); maze [1] [1] = '. '; printf ("the initial maze is (M * m)/n"); For (x = 1; x <= m; X ++) {printf ("/N"); For (y = 1; y <= m; y ++) printf ("% C ", maze [x] [Y]);} printf ("/N"); // start to do {gettop (S, P); OK = 0; I = 0; while (OK = 0 )&& (I <8) {x = P. X + move [I] [0]; y = P. Y + move [I] [1]; If (maze [x] [Y] = '0') {P. X = x; p. y = y; push (S, P); maze [x] [Y] = '. '; OK = 1;} I ++;} if (I = 8) {maze [p. x] [p. y] = '*'; POP (S, P) ;}} while ((! Stackempty (s) & (p. x! = M) | (P. y! = M); // output the path exploring result if (stackempty (s) printf ("no path/N"); else printf ("with path/N "); // output traces left by the exploration maze for (x = 1; x <= m; X ++) {printf ("/N"); For (y = 1; Y <= m; y ++) printf ("% C", maze [x] [Y]);} printf ("/N "); system ("pause ");}