Home > Back-end >  Why create a 2 d array of labyrinth is not closed?
Why create a 2 d array of labyrinth is not closed?

Time:09-23

# include & lt; stdio.h>
# include & lt; The malloc. H>
# include & lt; stdlib.h>
# include & lt; Time. H>
# define OK 1
# define TRUE 1
# define FALSE 0
# define the ERROR 0
# define OVERFLOW - 1
# define STACKINCREMENT 10
# define STACK_INIT_SIZE 200
//typedef int SElemType;
Typedef int the Status;
Typedef struct
{
int x;
int y;
} PosType;
Typedef struct
{
Int the word;
PosType seat;
Int di;
} SElemType;
Typedef struct
{
SElemType * top;
SElemType * base;
int stacksize;
} SqStack;
Int map [20] [20];
The Status InitStack (SqStack * s)//build the stack
{
S - & gt; The base=(SElemType *) malloc (STACK_INIT_SIZE * sizeof (SElemType));
if(! S - & gt; Base)
Return the ERROR;
S - & gt; Top=s - & gt; The base;
S - & gt; Stacksize=STACK_INIT_SIZE;
return OK;
}
The Status of Push (SqStack * s, SElemType e)//into the stack
{
If (s - & gt; Top - s - & gt; Base>=s - & gt; Stacksize)
{
S - & gt; The base=(SElemType *) realloc (s - & gt; Base, (s - & gt; Stacksize + STACKINCREMENT) * sizeof (SElemType));
if(! S - & gt; Base)
Return the ERROR;
S - & gt; Top=s - & gt; Base + s - & gt; Stacksize;
S - & gt; Stacksize +=STACKINCREMENT;
}
* s - & gt; Top++=e;
return OK;
}
Status of Pop (SqStack * s, SElemType * e)//the stack
{
If (s - & gt; Top==s - & gt; Base)
Return the ERROR;
=* * e - s - & gt; Top;
return OK;
}
The Status StackEmpty (SqStack s)//to empty
{
If (s.t op==s.b ase)
return TRUE;
The else
return FALSE;
}
Void Maze ()
{
//int map [20] [20].
Int I, j, k;
srand(time(NULL));
The map [1] [0]=map [1] [1]=map [18] [19]=0;
For (j=0; j<20; J++)
The map [0] [j]=map [19] [j]=1;
for(i=2; i<19. I++)
The map [I] [0]=map [I - 1] [19]=1;
for(i=1; i<19. I++)
{

for(j=1; j<19. J++)
{
K=rand () % 3.
If (k)
The map [I] [j]=0;
The else
The map [I] [j]=1;

}
//printf (" \ n ");
}

}
PosType NextPos (PosType * e, int d)
{
PosType q;
The switch (d)
{
Case 1: q.x=e - & gt; x;
Q.y=e - & gt; Y + 1;
break;
Case 2: q.x=e - & gt; X + 1;
Q.y=e - & gt; y;
break;
Case 3: q.x=e - & gt; x;
Q.y=e - & gt; Y - 1;
break;
Case 4: q.x=e - & gt; X - 1;
Q.y=e - & gt; y;
break;
}
The return of q;
}
The Status Pass (PosType e)
{
//int map [20] [20].
If (map [e.x] [e.y]==0)
return OK;
The else
Return OVERFLOW;
}
The Status FootPrint (PosType e)
{
//int map [20] [20].
The map [e.x] [e.y]=7;
return OK;
}
The Status MarkPrint (PosType e)
{
//int map [20] [20].
The map [e.x] [e.y]=3;
return OK;
}
Void PrintMaze ()
{
//int map [20] [20].
int i,j;
for(i=0; i<20; I++)
{
For (j=0; j<20; J++)
{
If (map [I] [j]==0)
Printf (" ");
Else if (map [I] [j]==1)
Printf (" 1 ");
Else if (map [I] [j]==3)
Printf (" 3 ");
Else if (map [I] [j]==7)
Printf (" 7 ");
}
}
}
The Status MazePath (PosType PosType start, end)
{
SqStack s;
PosType curpos;
InitStack (& amp; S);
SElemType e;
Int curstep;
Curpos=start;
Curstep=1;
Do {
If (Pass (curpos))
{
E.d I=1;
E.o rd=curstep;
E.s eat=curpos;
Push (& amp; S, e);
If (curpos. X==end. X& & Curpos. Y==end. Y)
Printf (" arrive ");
return TRUE;
Curpos=NextPos (& amp; Curpos, 1);
Curstep++;
}
The else
{
if(! StackEmpty (s))
{
Pop (& amp; S, & amp; E);
While (e.d I==4 & amp; & ! StackEmpty (s))
{
MarkPrint e.s (eat);
Pop (& amp; S, & amp; E);
}
If (e.d i<4)
{
E.d i++;
Push (& amp; S, e);
Curpos=NextPos (& amp; E.s eat, e.d I);
}
}
}
} while (! StackEmpty (s));
Printf (" can't reach the finish line ");
return FALSE;
}
The main ()
{

SqStack s;
PosType start and end;
Start. X=1; Start. The y=0;
End. X=18; End. Y=19;
InitStack (& amp; S);
Maze ();
MazePath (start, end);
PrintMaze ();
System (" pause ");
Maze ();
MazePath (start, end);
PrintMaze ();
System (" pause ");
return 0;
}

CodePudding user response:

Void PrintMaze ()
{
//int map [20] [20].
int i,j;
for(i=0; i<20; I++)
{
For (j=0; j<20; J++)
{
If (map [I] [j]==0)
Printf (" ");
Else if (map [I] [j]==1)
Printf (" 1 ");
Else if (map [I] [j]==3)
Printf (" 3 ");
Else if (map [I] [j]==7)
Printf (" 7 ");
}
printf (" \ n ");
}
}

CodePudding user response:

Inner loop per line of output, output a newline,

CodePudding user response:

I created a maze that module adds
Just wrap on it

CodePudding user response:

Print a maze, each line to line,nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related