Home > Back-end >  Pray god know!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Pray god know!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Time:09-28

#include
#include
using namespace std;

# define MAXLEN 30
# define the add 10
# define INISIZE 100
Typedef struct {
Int row;//a maze of rows
Int the column;//a maze columns
Int the grid [MAXLEN] [MAXLEN];//save maze state
} MazeType;//the labyrinth type
Typedef struct {
Int row;
Int the column;
} Coorddinate;//coordinates
Typedef struct {
Int the word;
Coorddinate seat;
Int di;
} MazeNode;//stack element type
Typedef MazeNode SElemType;
Typedef struct {
SElemType * base;
SElemtype * top;
int stacksize;
} the Stack;

Int IniStack (Stack * s) {
S - & gt; The base=(SElemType *) malloc (INISIZE * sizeof (SElemType));
if(! S - & gt; Base)
return -1;
S - & gt; Top=s - & gt; The base;
S - & gt; Stacksize=INISIZE;
}
Int StackEmpty (Stack * s) {
If (s - & gt; Top==s - & gt; Base)
return 1;
The else
return 0;
}
Int StackIsFull (Stack * s) {
If (s - & gt; Top - s - & gt; Base>=s - & gt; Stacksize)
return 1;
The else
return 0;
}
Int a Push (Stack * s, MazeNode mn) {
If (s - & gt; Top - s - & gt; Base>=s - & gt; Stacksize) {
S - & gt; The base=(SElemType *) malloc ((s - & gt; Stacksize + add) * sizeof (SElemType));
if(! S - & gt; Base)
return -1;
S - & gt; Top=s - & gt; Base + s - & gt; Stacksize;
S - & gt; Stacksize +=add;
}
* s - & gt; Top++=mn;
}
Pop (Stack is int * s, MazeNode * mn) {
If (s - & gt; The top!=s - & gt; Base) {
* mn=* - s - & gt; Top;
return 1;
}
The else
return 0;
}
Int DestroyStack (Stack * s) {
S - & gt; Top=s - & gt; The base;
return 1;
}

Int MazeInit (MazeType * maze) {
cout<" Please enter the maze of rows and columns: ";
Cin> Maze - & gt; Row> Maze - & gt; The column;
coutint i,j;
for(i=0; i<=maze - & gt; The column + 1; I++) {
Maze - & gt; The grid [0] [I]='1';
Maze - & gt; The grid [maze - & gt; row + 1] [I]='1';
}
for(i=0; iMaze - & gt; The grid [I] [0]='1';
Maze - & gt; The grid [I] [maze - & gt; the column + 1]='1';
}
cout<" Please enter the obstacle coordinates (end of the input coordinates is the (0, 0) input) : "& lt; int m,n;
Cin> M> N;
Do {
If (m> Maze - & gt; The row | | m<0 | | n> Maze - & gt; The column | | n<0 {
cout<" Enter the coordinates of the cross, please input again: "& lt; continue;
}
The else
Maze - & gt; The grid [m] [n]='1';
Cin> M> n;
(m==0} while & amp; & N==0);
return 1;
}
{int Pass (MazeType maze, Coorddinate pos)
If (maze. The grid [pos. Row] [pos. The column]=='0')
return 1;
The else
return 0;
}
{int MarkerPass (MazeType maze, Coorddinate pos)
Maze. The grid [pos. Row] [pos. The column]='2';
return 1;
}
Coorddinate NextCoord (Coorddinate pos, int I) {
The switch (I) {
Case 1:
Pos. The column +=1; break;
Case 2:
Pos. Row +=1; break;
Case 3:
Pos. The column -=1; break;
Case 4:
Pos. Row -=1; break;
Default:
exit(0);
}
Return pos.
}
{int MarkerNoPass (MazeType maze, Coorddinate pos)
Maze. The grid [pos. Row] [pos. The column]='3';
return 1;
}
Int MazePath (MazeType * maze, Coorddinate start, Coorddinate end) {
Coorddinate pos.
Stack s;
Int curstep;
MazeNode e;
IniStack (& amp; S);
Pos=start;
Do {
If (Pass (* maze, pos)) {
MarkerPass (* maze, pos);
E.o rd=curstep;
E.s eat=pos;
E.d I=1;
Push (& amp; S, e);
If (pos) row==end) row& & Pos. The column==end. The column) {
DestroyStack (& amp; S);
return 1;
}
The else {
Pos=NextCoord (pos, 1);
Curstep++;
}
}
The else {
if(! StackEmpty (& amp; S)) {
Pop (& amp; S, & amp; E);
If (e.d I==4 & amp; & ! StackEmpty (& amp; S)) {
MarkerNoPass (* maze, e.s eat);
Pop (& amp; S, & amp; E);
}
If (e.d i<4) {
E.d i++;
Push (& amp; S, e);
Pos=NextCoord (e.s eat, e.d I);
}

}
}
}while(! StackEmpty (& amp; S));
DestroyStack (& amp; S);
return 0;
}
Void PrintMaze (MazeType * maze) {
int i,j;
cout<" The radius said pathway "& lt; for(i=0; i<=maze - & gt; The row + 1; I++) {
for(j=0; j<=maze - & gt; The column + 1; J++) {
If (maze - & gt; The grid [I] [j]=='1')
cout<" This is a square ";
Else if (maze - & gt; The grid [I] [j]=='2')
cout<" This is a circular ";
The else
cout<" ";
}
cout}
}

Int main () {
MazeType maze;
Coorddinate start and end;
Char CMD.
cout<" To create a maze: "& lt; if(! MazeInit (& amp; Maze)) {
cout<" Create failure!" return -1;
}
Do {
cout<" Please input coordinates: ";
Cin> Start. Row> Start. The column;
coutIf (start. Row> Maze. Row | | start. Column> Maze. The column) {
cout<" Enter the coordinates of the cross, please input again: "& lt; continue;
}
} while (start. Row> Maze. Row | | start. Column> Maze. The column);
Do {
cout<" Please enter the coordinates of the export: ";
Cin> End. Row> End. The column;
coutIf (end. Row> Maze. Row | | end. Column> Maze. The column) {
cout<" Enter the coordinates of the cross, please input again: "& lt; continue;
}
} while (end. Row> nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related