Random Maze

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#define random(x) (rand()%x)
#define MaxSize 50
#define M 10
#define N 10

int mg [M + 2] [ N + 2] = {// global variables, initialization of the array
{}. 1
};
typedef struct {
int I;
int J;
int DI;
} Box;
typedef struct {
Box Data [the MaxSize];
Top int;
} StType;
void InitStack (StType & S *) // initialize stack
{
S = (StType *) the malloc (the sizeof (StType));
S-> Top = -1;
}
BOOL the Push (StType & S *, Box E ) // push
{
IF (S-> Top-== the MaxSize. 1)
return to false;
S-> Top ++;
S-> Data [S-> Top] = E;
return to true;
}
BOOL StackEmpty (StType S *) // empty judgment
{
return (S-> Top == -. 1);
}
BOOL getTop (StType & S *, Box & E) // get the top element
{
IF (S-> Top == -. 1)
to false return;
E = S-> Data [S-> Top];
return to true;
}
BOOL Pop (StType & S *, Box & E) // popped
{
IF (S-> Top == -. 1)
return to false;
E S- => Data [S-> Top];
S-> top--;
return to true;
}
void DestroyStack (StType & S *) // stack destruction
{
Free (S);
}
BOOL mgpath (XI int, int Yi, int xe, int ye) {// solving path (XI, Yi) -> (XE, YE)
Box path [the MaxSize], E;
int I, J, DI, I1, J1, K;
BOOL Find;
StType * st; // define the stack ST
InitStack (ST); // initialize stack pointer
ei = xi; ej = yi; e.di = -1; // set inlet e
Push (st, e); // block e the push
mg [xi] [yi] = - 1; // set the value of the inlet labyrinth is -1, the block avoid repetition come
while (StackEmpty (st)!) // stack cycles
{getTop (st, e ); // get stack block e
EI = I; J = EJ; DI = e.di;
IF (J == I == && XE YE) {// find the exit, the output path
printf ( "a labyrinth path is as follows: \ n-");
K = 0 ;
the while {(StackEmpty (ST)!)
Pop (ST, e); // block out of the stack e
path [K ++] = e; // add path e to the array

}
the while (K> =. 1) {
- K- -;
IF (path [K] .i-path [K +. 1] == .i. 1) {// print direction
the printf ( "(↓)");
}
IF (path [K] .i-path [K + 1'd] .i == -. 1) {
the printf ( "(↑)");
}
IF (path [K]. J-path [K +. 1] ==. J. 1) {
the printf ( "(→)" );
}
IF (path [K]. J-path [K +. 1]. J == -. 1) {
the printf ( "(←)");
}
the printf ( "\ T (% D,% D)", path [K] .i, path [K]. J);
IF ((K + 2). 5% == 0) // after every five square transducer output line
printf ( "\ n");
}
printf("\n");
DestroyStack (st); // stack destruction
return true; // returns true output after a labyrinthine path
}
Find = to false;
(! DI <&& Find. 4) {// find the while block (i, j) of the next adjacent to go block (I1, J1)
DI ++;
Switch (DI) {
Case 0: I1 = I1; J1 = J; BREAK;
Case. 1: I1 = I; J = J1 +. 1; BREAK;
Case 2: I1 = +. 1 I; J1 = J; BREAK;
Case. 3: I1 = I; J1 = J1; BREAK;
}
IF (mg [I1] [J1] == 0) // to go to find a neighboring block, provided find true
find = to true;
}
IF (find) // to go find a neighboring block (I1, J1)
{
ST-> Data [ST-> Top] DI = .di; // modify the original top element the di value
EI = I1; EJ = J1; e.di = -1;
the push (ST, e); // e adjacent block to go into the stack
mg [i1] [j1] = - 1; // the ( i1, j1) is set to the value -1 into the labyrinth, to avoid repetition of the block come
}
the else // no path stack is back to go
{
Pop (ST, E); // the stack block unstack
mg [e, i] [e , j] = 0; // make unstack position to block the other paths to go block
}
}
DestroyStack (ST); // destroy stack
return false; // indicates no go path, Back to false
}
void Print () {// print labyrinth arrangement map
int I, J;
for (I = 0; I <M + 2; I ++) {
for (J = 0; J <N + 2; J ++) {
IF (mg [I] [J] ==. 1)
the printf ( "█");
the else
the printf ( "");
}
the printf ( "\ n-");
}
}
void Putin () {
int I, J, K = 30; // k denotes the number of the obstacle
srand ((int) time (0 )); // random function
for (I = 0; I <M + 2; I ++) {
for (J = 0; J <N +2; J ++) {
IF (I == I == 0 || ||. 1 J M + J == N == 0 || +. 1) // set the border
mg [I] [J] =. 1;
}
}
for (I = 0; I <K; I ++) {// generate random locations obstacle
mg [random (10)] [random (10)] =. 1;
}
mg [1] [1] = 0; // start and end points can not be an obstacle
mg [. 9] [. 9] = 0;
}
void SET () {// transition
Putin ();
Print ();
IF (mgpath! (1,1, M, N))
the printf ( "no solution to the problem maze \ n-!");

}
void AA () {
int n-;
the printf ( "labyrinth printing press 1, press exit 0 \ n" );
Scanf ( "% D", & n-);
IF (n-) {
System ( "CLS");
Memset (mg, 0, the sizeof (mg)); // clears the array mg
SET ();
AA () ;

} the else return; // main function returns
}
int main () {

AA ();
return. 1;

}

Guess you like

Origin www.cnblogs.com/mt2018210328/p/12078662.html