Course Design of "Data Structure"-Maze Solution

This was written by myself last year, and the release is now for reference only

1 Design goal
Maze problem: Write a program to solve the maze problem. The maze is represented by a long square matrix with m rows and n columns, and 0 and 1 represent the pathways and obstacles in the maze, respectively. Design a program to find a path from the entrance to the exit for an arbitrarily set labyrinth, or conclude that there is no path. The main points of the algorithm: randomly create a maze, observe the maze of different difficulty, and give a path solution of the maze when choosing help.
2 Project analysis and design
2.1 Project needs analysis
1. This program realizes the exploration process of the maze. In the way that the user and the computer talk, that is, after the "prompt information" is displayed on the computer terminal, the user enters the demo program on the keyboard The specified operation command, then the program will explore the path and output the path.
2. In this demo program, the input form ends with "carriage return", and repeated characters are allowed. 
3. The two-dimensional array is used to store the position of the maze, and the stack is used to store the exploration path. Each node contains three integer variables. The input form ends with Enter.
4, in this procedure, the user may enter the labyrinth of arbitrary size between (3-100), and then automatically generate random maze, help in the choice of the user, the system automatically find the path and outputs the maze
2.2 storing design
data Storage structure: Also known as the physical structure of data, it is the realization of the logical structure of data in a computer. It should be pointed out that the logical structure of the data is to analyze the data from the logical relationship between the data elements, has nothing to do with the specific storage of the data, and is independent of the computer. The data storage structure is computer dependent, including the storage representation of data element values ​​in the computer and the storage representation of logical relationships in the computer. The storage representation of the logical relationship between data elements in the computer is divided into the following four types:
sequential storage mode: sequential storage mode refers to placing all data elements in a continuous storage space and making logically adjacent data elements The corresponding physical storage locations are also adjacent (that is, to ensure that the logical position relationship is consistent with the physical position relationship). Sequential storage structures are usually implemented with the aid of arrays in programming languages.
Chained storage method: The chained storage method does not require logically adjacent elements to be stored in physical positions adjacent to each other, that is to say, the storage of data elements is arbitrary. The storage representation corresponding to each data element consists of two parts, one part stores the element value itself, and the other part is used to store pointers representing logical relationships. We can think of the pointer as the storage address of the next data element.
Index storage mode: The index storage mode adds an index table while storing data elements. Each item in the index table includes a keyword and an address. The keyword is a data item that can uniquely identify a data element. The address indicates the storage address of the data element or the first address of the storage area.
Hash storage method: Hash storage is also known as hash storage. This storage method stores data elements in a continuous area. The specific storage location of each data element is based on the key value of the data. Hash) function directly calculated.
This experiment uses sequential storage.
2.3 Algorithm design
1. Define the size of the labyrinth type, you can customize to modify #define SIZE 102 // The maximum range of the labyrinth
2. Create a structure to store the array information (the horizontal coordinate X of the array, the vertical coordinate Y of the array, Direction dir)
typedef struct {int x; int y;} PosType; // Coordinate position
typedef struct {PosType seat; // "Coordinate position
" of the channel block in the maze int di; // From the previous channel block to this channel Block "direction"}
SElemType;
3. Create random maze
void Random (int (* mg) [SIZE],
int size, PosType start,
PosType end); void PrintMaze (int (* mg)
[SIZE], int size); // Print the maze Status Check (char & choice);
// Confirm that the input is correct int main () {
stack s;
int mg ​​[SIZE] [SIZE] = {1}, size;
PosType start, end; char choice;
system (“mode con cols = 220 lines = 220”);
4. Set the printing path
Status MarkPath (PosType e, int ( mg) [SIZE],
int di); PosType FrontPos (PosType e,
int dir );
Status PathPrint (stack s, int ( mg) [SIZE]);
PosType NextPos (PosType e, int dir) {PosType E;
switch (dir) {case 1: Ex = e.x + 1; // right
Ey = ey;
break;
case 2: Ex = ex; // downward
Ey = e.y + 1; break;
case 3: Ex = ex-1; // leftward Ey = ey; break;
case 4: Ex = ex; // Up Ey = ey-1; break;
} return E;}
5 Summary
5.1 Gaining the course design during this period, I have a deeper understanding of the application of computers, the role of data structures and the use of C language. Especially the progress of the C language makes me deeply feel that any knowledge I need needs to be practiced. Without practice, I cannot truly understand this knowledge and master it, making it my own wealth. In all aspects of theoretical learning and hands-on practice, I learned a lot through independent learning and teacher consultation. Of course, I also encountered a lot of problems, and it is precisely because of the thinking caused by these problems that I have gained. Write the labyrinth and create it manually at the beginning, but the
output of the labyrinth of 50 50 100100 is too time-consuming, so it is changed to automatically generate the labyrinth automatically
, void Random (int (* mg)
[SIZE], int size, PosType start, PosType end);
But the problem is coming, because the randomness is too large, so that each randomly generated maze is unsolvable, that is, there are too many obstacles, resulting in many times that the starting point and the end point are obstacles without roads, so the ratio of roads to obstacles must be adjusted , So there is
for (i = 1; i <size-1; i ++)
for (j = 1; j <size-1; j ++) {
k = rand ()% 4; // randomly generate 0, 1, 2, 4 Three numbers
if (k) mg [i] [j] = 0;
else {
mg [i] [j] = 1;} // else
}
Change the obstacle by setting the number of k = rand ()% 4; The ratio with the road is generally 3-5. If it is higher than five, the probability of having a road is close to 100%, and the probability of 3 having a solution is slightly lower, so choose 4, the probability of having a solution and no solution is equal. Don't use the
source code, it gives you so many ideas

Published 23 original articles · Like 13 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/huayula/article/details/93709561