Maze MAZE (data structure)

xiaoxiao2021-03-06  34

#include

#include

#include

#define Edge 10

#define Plus 5

#define sta_size 100

#define startX 1

#define starty 1

Typedef struct datastore {

INT X, Y, D, LSX, LSY;

Bool NL;

} Datastore, * link; // Store walking road

DataStore InitDataStore (Datastore & D) {

D.x = d.lsx = startX;

D.y = d.lsy = starty;

D.D = 4;

D. NL = false;

Return D;

}

Typedef struct {

Link base;

Link top;

Int stacksize;

// count records the number of real-time data in the stack

INT country;

// use the array array to record whether the labyrinth path has been accessed to prevent walking

Bool array [edge * Edge];

} SQSTACK; / / Stack

Bool Push (Sqstack & S, Datastore & E)

{

IF (S.Top - S.Base> = S.STACKSIZE) {

S.BASE = (LINK) Realloc (s.stacksize plus) * sizeof (datastore));

IF (! s.base) Return False;

S.TOP = S.BASE S.STACKSIZE;

S.STACKSIZE = Plus;

}

* ( S.top) = E;

S.count ;

Return True;

} // push function

BOOL POP (Sqstack & S, Datastore & E) {

IF (S.TOP == S.Base) Return False;

e = * (- S.top);

S.count -;

Return True;

} // POP function

Bool initstack (Sqstack & s) {

S.BASE = (link) malloc (sta_size * sizeof (datastore));

IF (! s.base) Return False;

S.TOP = S.BASE;

S.stacksize = sta_size;

S.count = 0;

For (int i = 0; i

S.Array [i] = false;

Return True;

} // Construct a new stack S

Bool Destroystack (Sqstack & S) {

IF (! s.base) Return False;

For (int i = 0; i

Free (S.TOP);

Return True;

}

Bool nextpos (int A [], Sqstack & S, Datastore & E) {

// Case to be judged, whether the location is the original position

// As long as there is no test, continue to test until the direction D = 0

For (; e.d> = 0;)

{

Switch (e.d)

{

Case 4:

// Right

/ / Direction 1, eliminate the direction of walking

e.d--

// If the next step is the road (0) or outlet (-1), and if it is the road, it is not the previous position of the current location, then confirm and record the current test position is the latter position // If the condition is not satisfied, It is a wall, then judge the next direction

IF (A [S.TOP-> X * Edge (S.TOP-> Y 1)] <= 0 && s.top-> lsy! = (EY 1) && S.Array [S.TOP- > x * Edge (S.TOP-> Y 1)] == false)

{

E.Y ;

S.Array [S.TOP-> x * Edge (S.TOP-> Y 1)] = true;

Return True;

}

Break;

Case 3:

// Down

e.d--

IF (a [(S.top-> x 1) * Edge S.TOP-> Y] <= 0 && S.top-> Lsx! = (EX 1) && S.Array [(S.TOP -> x 1) * Edge S.TOP-> Y] == false)

{

E.x ;

S.Array [(S.TOP-> X 1) * Edge S.top-> Y] = true;

Return True;

}

Break;

Case 2:

// Left

e.d--

IF (a [S.top-> x * Edge (S.top-> Y - 1)] <= 0 && s.top-> lsy! = (EY - 1) && s.Array [S.TOP- > x * Edge (S.TOP-> Y - 1)] == false)

{

E.Y -;

S.Array [S.TOP-> x * Edge (S.TOP-> Y - 1)] = true;

Return True;

}

Break;

Case 1:

// Up

e.d--

IF (A [(S.TOP-> x - 1) * Edge S.TOP-> Y] <= 0 && S.top-> Lsx! = (EX - 1) && S.Array [(S.Top -> x - 1) * Edge S.TOP-> Y] == false)

{

E.X--

S.Array [(S.TOP-> X - 1) * Edge S.top-> Y] = true;

Return True;

}

Break;

/ *

// If the four directions are judged to fail, the current road segment is labyrinth, and the NL is true, that is, the road is at the end.

Else

{

S.top-> nl = true;

// cout << "Current road to the end ..." << Endl;

Return False;

} B

/ * /

// *

DEFAULT:

S.top-> nl = true;

Return False;

// * /

}

}

Return False;

}

Void Patch (Int a [], Datastore & D, Sqstack & S) {

// a [D.x * Edge D.Y];

While (a [S.top-> x * Edge S.TOP-> Y]! = -1)

{

IF (NextPOS (A, S, D)) {

Push (s, d);

// The front direction is the direction of S.TOP

(S.TOP-1) -> d = S.top-> D;

// The current TOP point does not allocate the direction

S.top-> D = D.D = 4;

// Put the current top X and Y to the next coordinate recording area

D.lsx = S.top-> x;

D.LSY = S.top-> y;

}

Else

{

While (S.TOP-> NL)

{

DataStore TD;

// If there are four directions fail, NL is true, out of the stack

POP (S, TD);

D = * S.TOP;

D.LSX = D.x;

D.lsy = D.Y;

/ *

// After the stack, the top pointer retracts, and puts the direction D of the TOP pointer to the next temporary variable D.

D.d = S.top-> D;

// After the stack, replace the coordinates of the D later to process into the coordinates of the current TOP pointer

// Put the coordinate of the current location to the next process D memory coordinates

D.lsx = D.x = S.top-> x;

D.lsy = D.Y = S.TOP-> Y;

// * /

}

IF (s.top-> x == startX && s.top-> y == startY)

{

// If the stack is started to the current location, then the maze does not solve

Cout << "Error! Maze does not solve" << Endl;

Return;

}

}

}

//a[d.x*EDGE D.Y] = 1;

}

/ / Judgment direction connection

// *

Void JuGedir (Link Top, Char Dir [])

{

Switch (TOP-> D)

{

Case 3:

STRCPY (Dir, "Right ");

Break;

Case 2:

STRCPY (DIR, "Down ");

Break;

Case 1:

STRCPY (DIR, "Left ");

Break;

Case 0:

STRCPY (DIR, "UP ");

}

}

// * /

Void Printmaze (Sqstack & S) {

SQSTACK SQ;

INITSTACK (SQ);

// reverse the stack

For (; s.top! = s.base; s.top -)

{

* ( sq.top) = * S.TOP;

}

Sq.count = 1; // Do a form

// Sequential output results in the new stack established in the function

For (; sq.top! = sq.base; sq.top -, sq.count )

{

Char Dir [10];

Jugedir (SQ.TOP, DIR);

Cout << "(" << Sq.top-> x << "," << Sq.top-> Y << "," << DIR << ")" << "/ t";

IF (sq.count% 2 == 0)

Cout << Endl;

}

Cout << Endl;

Cout << "Go" << S.count << "step ..." << Endl;

}

void main ()

{

INT MAZE [EDGE] [EDGE] = {// 1 2 3 4 5 6 7 8

{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},

{1, 0, 1, 0, 1, 0, 1, 1, 1, 1}, // 1

{1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, // 2

{1, 1, 1, 1, 1, 1, 0, 1, 0, 1}, // 3

{1, 0, 0, 0, 0, 0, 0, 1, 0, 1}, // 4

{1, 0, 1, 0, 1, 1, 1, 1, 0, 1}, //5

{1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, //6

{1, 0, 1, 1, 1, 1, 1, 1, 1, 1}, // 7

{1, 0, 0, 0, 0, 0, 0, 0, -1, 1}, // 8

{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}

}

// *

For (int i = 0; i

For (int J = 0; j

{

IF (j == 0) cout << Endl;

COUT << Maze [i] [j] << "

}

Cout << Endl;

// * /

DataStore D;

InitDataStore (D);

SQSTACK S;

INITSTACK (S);

Push (s, d);

Patch (Maze [0], D, S);

PRINTMAZE (S);

// deStroyStack (s);

}

转载请注明原文地址:https://www.9cbs.com/read-78094.html

New Post(0)