Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Searching for the shortest path in the maze with C++
Searching for the shortest path in the maze with C++
The QQ you are looking for is 85 14 15 12, which is mine.

I surf the internet in the company, but I can't get on QQ. sorry

If you have any questions, please send them to Baidu in my message.

It's all about data structures.

I found it and posted it:

# include & ltiostream.h & gt

# include & ltstdlib.h & gt

# include & ltiomanip.h & gt

# define stack _ init _ size100//initial stack size

# definestackincrement10//Increase the length of the stack.

# Define the height of the MAPH 20 // maze.

# Define the size of MAPW 20 // maze length.

////////////////////////////////////////////////////////////////////////////////

//structure name: MazeCell

//Function: used to describe the information of maze components.

//Member: Pass: when Pass is 1, it indicates a conductive block; 0 indicates an obstacle block;

// Footprint: When the footprint is 1, it means that a footprint has been left, otherwise it means that it has not passed through here.

////////////////////////////////////////////////////////////////////////////////

Typedef structure

{

int Pass

Boolean footprint;

} Mazesel;

////////////////////////////////////////////////////////////////////////////////

//structural name: SElemType

//Function: This is an element of the stack to indicate the current position (the stack is used to describe the current path).

//Member: ord: the serial number of the channel block.

// x: the abscissa of the current position

// y: the ordinate of the current position

// di: search direction 1 east 2 south 3 west 4 north.

////////////////////////////////////////////////////////////////////////////////

Typedef structure

{

Int order;

int x;

int y;

int di

} SElemType

////////////////////////////////////////////////////////////////////////////////

//Structure name: SqTack

//Function: This is the stack used to record the current path.

//Member: * Pointer at the bottom of the base stack, pointing to the starting point.

// *top stack top pointer, pointing to the end of the path.

Capacity of stacksize stack

////////////////////////////////////////////////////////////////////////////////

Typedef structure

{

SElemType * base

SElemType * top

int stacksize

} SqStack

////////////////////////////////////////////////////////////////////////////////

//Structure name: Seat

//function: used to record maze coordinates. This structure is an intermediate variable, which is established purely for programming convenience.

//Member: X: used to record abscissa.

// y: used to record the ordinate.

////////////////////////////////////////////////////////////////////////////////

Typedef structure

{

int x;

int y;

} seat;

////////////////////////////////////////////////////////////////////////////////

//Function name: InitStack

//function: this function is used to initialize a stack, that is, to find a stack with the size of STACK_INIT_SIZE in the class memory.

//element and assign its first address to the bottom pointer of the stack. At this point, the top pointer of the stack coincides with the bottom pointer. Stack capacity

//The quantity is STACK_INIT_SIZE. If the operation is successful, the function returns 1, and if the class storage space is insufficient, the function returns.

// 0, indicating that initialization failed.

//Function parameter: sqstack &; S

//Function return value type: bool

////////////////////////////////////////////////////////////////////////////////

bool init stack(sq stack & amp; s)

{

s . base =(selem type *)malloc(STACK _ INIT _ SIZE * sizeof(selem type));

If (! American base)

{

Returns 0;

}

s . top = s . base;

S.stacksize = STACK _ INIT _ SIZE

Returns 0;

}

////////////////////////////////////////////////////////////////////////////////

//function name: BuideMaze

//Function: The function is to create a maze map according to the user's requirements and transform the plastic array input by the user.

//The shape is a maze array.

//

//function parameters: maze unit diagram [size _ of _ map] [size _ of _ map]

//Seat & start coordinates

//Seat & Terminal Coordinates

//Function return value type: 1 if bool is successfully established, otherwise 0 is returned.

////////////////////////////////////////////////////////////////////////////////

Void BuideMaze (Map size of Mazesel map ][MAPW size], int ma[MAPH size ][MAPW size])

{

for(int I = 0; The size of me & ltMAPH; i++)

{

for(int j = 0; The size of j & ltMAPW; j++)

{

Map [i][j] pass = ma[I][j];

Map [i][j] Footprint = 0;

}

}

}

////////////////////////////////////////////////////////////////////////////////

//function name: Pass

//Function: This function is used to judge whether the current point is reachable. If accessible, return 1, otherwise return 0.

//The so-called passability means that the current position is a conductive block and there is no footprint left at the current position.

//function parameter: the coordinates of the current block of Seat curpos.

//maze unit map [map size] [map size] maze map

//Function return value type: bool returns 1 if the current block can be generalized, otherwise it returns 0.

////////////////////////////////////////////////////////////////////////////////

Boolean Pass (Map size of the map of Seikulpos, Mazesel ][MAPW size])

{

If (mapping [curpos.x][curpos.y]. Pass ==0)

{

Returns 0;

}

else if(Map[curpos.x][curpos.y]。 Footprint == 1)

{

Returns 0;

}

other

{

Returns1;

}

}

////////////////////////////////////////////////////////////////////////////////

//function name: FootPrint

//Function: This function is used to leave footprints under the current path.

//Function parameter: the current coordinates of the seat curpos.

//maze unit map [map size] [map size] maze map

//Function return value type: None

////////////////////////////////////////////////////////////////////////////////

Empty footprint (Seat curpos, Map size of MazeCell map ][MAPW size])

{

Map[curpos.x][curpos.y]。 Footprint =1;

}

Boolean push (SqStack & amps, SElemType e)// stack insert function

{

if(s . top-s . base & gt; =S.stacksize)

{

s . base =(selem type *)realloc(s . base,(s . stack size+stack increament)* sizeof(selem type));

If (! American base)

{

Returns 0;

}

s . top = s . base+s . stack size;

s . stack size = s . stack size+stack increation;

}

* s . top++ = e;

Returns1;

}

bool Pop(SqStack & amp; S, select the type & ampE)///Stack to take out the top element and assign the value to E.

{

If(S.base==S.top) returns false.

s . top-;

e = * S.top

Return true

}

////////////////////////////////////////////////////////////////////////////////

//Function name: NextPos

//function: this function is used to switch the position in the di direction with the coordinates of x, y to the current position.

//Function parameter: the current coordinates of the seat curpos.

// int di direction

// int x, y coordinates

//Function return value type: None

////////////////////////////////////////////////////////////////////////////////

void NextPos(int x,int y,Seat & ampcurpos,int di)

{

if(di== 1)

{

curpos . y = y+ 1;

curpos.x = x

}

else if(di==2)

{

curpos . x = x+ 1;

curpos.y = y

}

else if(di==3)

{

curpos . y = y- 1;

curpos.x = x

}

else if(di==4)

{

curpos . x = x- 1;

curpos.y = y

}

}

///////////////////////////////////////////////////////////////////////////

//Function name: Putin

//function: Enter elements into an array.

//Function parameter: int map[] gives the result array to be mapped.

//The length of Intwei maze

Height of Inthig maze

//Function return value type: None

///////////////////////////////////////////////////////////////////////////

Void Putin (int map[], int&Wei International Trading Co., Ltd.; High)

{

int w,h;

Cout & lt& lt If the maze you want to enter is larger than the specified size, you only need to modify the size of MAPH and the size of the map.

Cout & lt& lt "Please enter the length of the maze (the length is less than or equal to".

CIN & gt; & gtw;

Cout & lt& lt "Please enter the height of the maze (the height is less than or equal to".

CIN & gt; & gth;

Cout < < "1stands for conductive block; 0 indicates an obstacle block. "

Wei = w;;

hig = h;

for(int I = 0; The size of me & ltMAPH; i++)

for(int j = 0; The size of j & ltMAPW; j++)

*(map+I * SIZE _ OF _ MAPW+j)= 0;

for(int is = 1; is & lth+ 1; Yes++)

{

Cout & lt& lt "Please enter the first one"

for(int js = 1; js & ltw+ 1; js++)

{

Int point;

CIN & gt; & gt point;

*(map+is * SIZE _ OF _ MAPW+js)= point;

}

cout & lt& ltendl

}

}

///////////////////////////////////////////////////////////////////////////

//Function name: display

//function: used to display all the elements in the stack.

//Function Parameter: None

//Function return value type: None

///////////////////////////////////////////////////////////////////////////

Empty display (SqStack S)

{

int I = 1;

selem type * p;

p = s . base;

Cout & lt& lt "From bottom to top:"

Cout & lt& lt" di search direction: di= 1 east, di=2 south, di=3 west, di=4 north "< & ltendl.

And (p! =S.top) // Print elements from bottom to top.

{

Cout & lt& lt "first"

cout & lt& lt"(" & lt& lt(*p)。 y;

cout & lt& lt“,”& lt& lt(*p)。 x;

cout & lt& lt“,”& lt& lt(*p)。 di & lt& lt”)" & lt& ltendl

p++;

i++;

}

}

Void display maze(int ma[MAPH size ][MAPW size], int w, int h)

{

Cout & lt& lt "The maze is (1 means that it can pass; 0 means can't pass): "

for(int I = 0; I<h+2; i++)

{

for(int j = 0; j & ltw+2; j++)

{

cout & lt& ltma[I][j]& lt; & lt" ";

}

cout & lt& ltendl

}

cout & lt& ltendl

}

///////////////////////////////////////////////////////////////////////////

//function name: MazePath

//Function: Organize all sub-functions and solve the maze.

//Function Parameter: None

//Function return value type: 1 If the int maze has a solution, no solution is 0;

////////////////////////////////////////////////////////////////////////////

Int maze path(maze cell Map[MAPH size ][MAPW size], seat starting point, seat ending point)

{

Choose type e;

sq stack S; //Define a stack

InitStack// Initialize the stack

Seat bending; //Define the current position

Int curstep// pedometer

curstep = 1; //pedometer 1

curpos . x = start . x; //

curpos . y = start . y; //Set the current position as the starting point

Cout & lt& lt "starting point is:"

Cout & lt& lt "The destination is:"

///////////////////////////////////////////////////////////////////////////

//

//The following loop is the core program to solve the maze.

////////////////////////////////////////////////////////////////////////////

do

{

If(Pass(curpos, Map)) // If the current block is accessible, please do the following.

{

Footprint (curpos, map); //Leave a footprint

E.ord = curstep// Write down the pedometer.

e . x = curpos . x; //Write down the number of lines

e . y = curpos . y; //Write down the following figures

e . di = 1; //Let one direction be the East.

Push (s, e); //Stack operation, including the current position in the current path.

if(curpos . x = = end . x & amp; & curpos.y = = end.y)//If the current block is the end point, perform the following operations.

{ //

Cout & lt& lt "OK!" ; //

Display; //Call the subroutine of the display stack to display the results.

Returns1; The//function returns 1.

} //

Else // If the current position is not the end point, do the following.

{ //

NextPos(curpos.x,curpos.y,curpos, 1); //Switch the position in the east direction to the current position.

cur step++; //pedometer added 1.

}

}//If

other

{

If (S.base! =S.top) // If the current block is impassable, the stack is not empty (meaning there is still a solution).

{

Pop(S,e); //Pop up the top element of the stack for observation.

while(e . di = = 4 & amp; & (s.top-s.base))//If all four elements at the top of the stack are blocked.

{

Pop(S,e); //Pop up the next stack top element for observation.

}//When

if(e . di & lt; 4) // If the stack top element is not passed in other directions,

{ //

e . di++; //Switch the next direction to the current direction.

Push (s, e); //Push on the stack

NextPos(e.x,e.y,curpos,e . di); //Switch the next direction position to the current position.

}//If

}//If

}//Otherwise

}while(S.base! = s . top); //As long as the stack is not empty, it means that there is a solution and the loop is repeated.

Cout & lt& lt "Path not found"

}

////////////////////////////////////////////////////////////////////////////////

//Function name: main

//Function: Organize all sub-functions and solve the maze.

//Function Parameter: None

//Function return value type: bool maze returns 1 if there is a solution, otherwise it returns 0;

//

////////////////////////////////////////////////////////////////////////////////

void main()

{

Mazesell Map size ][MAPW size]; //Define a maze array

/* int migong[MAPH size ][MAPW size] = {

{ 0,0,0,0,0,0,0,0,0,0},

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

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

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

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

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

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

{ 0,0,0,0,0,0,0,0,0,0},

}; //maze represented by array */

int w,h;

Int migong[MAPH size ][MAPW size];

Putin (Mi Gong [0], W, H);

BuideMaze (map, Migong); //Call the sub-function that creates the maze.

DisplayMaze (mi gong, w, h); //Displays the shape of the maze.

The seat begins and ends; //Define the structure of current position, start position and end position.

/* start . x = 1; //Starting point

start . y = 1; //

end . x = 2; //End point

end . y = 2; //*/

Cout < < "abscissa of starting point:";

CIN & gt; & gtstart.y

Cout < < "Starting point ordinate:";

CIN & gt; & gtstart.x

Cout < < "End abscissa:";

CIN & gt; & gtend.y

Cout < < "Ending ordinate:";

CIN & gt; & gtend.x

MazePath (mapping, start, end);

}

Most programs are divided by function. You just need to figure out the function, and that's it.

Wish you success! I spent three days writing it.