#include <iostream>
using namespace Std;
void EnQueue (int i,int j,int k); Queue a Node
void DeQueue (int *i,int *j,int *k); Gets the ordinal and corresponding maze coordinates of the current node, and then Dequeue
BOOL Getnextpos (int *i, int *j,int count); Get the location of the next adjacency point
void Shortestpath_bfs (int i,int j); Breadth-first traversal to find the shortest path
void Shortestpath (); Output Shortest Path
void Print (); Output Maze shape
int map[10][10] = {{1,1,1,1,1,1,1,1,1,1},{1,0,0,1,0,0,0,1,0,1},{1,0,0,1,0,0,0,1,0,1},{1,0,0,0,0,1,1,0,0,1}, { 1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},{1,0,1,0,0,0,1,0,0,1},{1,0,1,1,1,0,1,1,0,1},{1,1,0,0,0,0,0,0,0,0},{1,1,1,1,1,1,1,1,1,1}} ;
struct Node
{
int parent_id; Save the location of the parent node
int node_id; The ordinal of the current node to pass to the child node
int x, y; The coordinates of the current node.
}Q[10*10]; Each node contains the maze coordinates, the ordinal number in the queue, the ordinal of the parent node, and multiple nodes forming a queue
int front = 0,rear = 0; Queue head and tail pointers
void Main ()
{
cout<< "Program Description:" << ' \ n ' << "1. The output path is the shortest path;" << ' \ n ' << "2. The default exit is in the bottom right corner and can be adjusted if necessary. << ' \ n ' << ' \ n ';
cout<< "initial map as follows:" <<endl;
Print ();
int i,j;
reinput:cout<< "Please enter the starting coordinates (x, y):" <<endl; cin>>i>>j;
if (Map[i][j])
{
cout<< "cannot depart from this place, please re-enter! "<<endl;goto Reinput;
}
SHORTESTPATH_BFS (I,J); cout<< "One of the shortest paths is as follows:" <<endl;
Shortestpath ();
}
void EnQueue (int i,int j,int k)//Queue a node
{
q[rear].x = i;
Q[rear].y = j; Save the coordinate position of the current node
q[rear].parent_id = k; Save the parent node ordinal ************-1
q[rear].node_id = rear; Save current node ordinal
rear++;
}
void DeQueue (int *i,int *j,int *k)//Gets the ordinal and corresponding maze coordinates of the current node and then dequeue
{
*i = q[front].x;
*j = Q[FRONT].Y;
*k = q[front].node_id;
front++; Dequeue a node
}
BOOL Getnextpos (int *i, int *j,int count)//Get position of next adjacency Point
{
Switch (count)
{
Case 1: (*j) + +; return 1; Right
Case 2: (*i) + +; return 1; Under
Case 3: (*J)--; return 1; Left
Case 4: (*i)--; return 1; On
Default
return 0;
}
}
void Shortestpath_bfs (int i, int j)//breadth-first traversal search for shortest path
{
int count,m,n,k;
EnQueue (i,j,-1);
MAP[I][J] = 1; Start the queue, mark the beginning has passed
while (true)
{
Count = 1;
DeQueue (&i,&j,&k);
n = i,m = j;
Save Current location
while (Getnextpos (&i,&j,count))
{
count++;
if (! MAP[I][J])
{
EnQueue (I,J,K);
MAP[I][J] = 1;
if (i = = 8 && J = = 9)
Return Reach end point (8,9) is the default endpoint and can be modified arbitrarily
}
i = n; j = m; Guaranteed traversal of all adjacent positions of the current coordinates
}
}
}
void Shortestpath ()
{
int i,j,k,sum=0;
K = rear-1;
while (k! =-1)
{
i = q[k].x;
j = q[k].y;
MAP[I][J] = 2;
K = q[k].parent_id;
}
cout<< "0 1 2 3 4 5 6 7 8 9" <<endl;
for (i = 0;i < 10;i++)
{
cout<<i;
for (j = 0;j < 10;j++)
{
if (map[i][j]==2)
{sum++; cout<< "-";}
Else
cout<< "";
}
cout<<endl;
}
cout<< "Shortest path length:" <<sum<<endl;
}
void Print ()
{
cout<< "0 1 2 3 4 5 6 7 8 9" <<endl;
for (int i = 0;i < 10;i++)
{
cout<<i;
for (int j = 0;j < 10;j++)
{
if (Map[i][j])
cout<< "";
Else
cout<< "-";
}
cout<<endl;
}
}
Using C + + to achieve maze, the shortest path, breadth first traversal, queue, to understand it, you have mastered the data structure of the most commonly used algorithms (C language can also be understood)