Also talk about the maze algorithm (shortest path queue) + source program

zhaozj2021-02-16  57

Dizzy bird, there is a mistake, changed a judgment condition, plus a variable. The idea is right, but it is also a wide range of traversal.

I have changed now, I found the error, please correct: sduboy@163.com#include #include #define elemType MazepostypedEf struct mazepos0 {int x, y;} mazepos; typedef struct maze0 {int s; // pass or wall MazePos pre; bool visited;} Maze; typedef struct QueueNode {elemType data; struct QueueNode * next;} queuenode; typedef struct queue {struct QueueNode * front; struct QueueNode * rear;} Queue; Maze maze [10] [10]; / / ============================================ ==== queue * initQueue () {queue * q = null; if (! (Q = (queue *) malloc (sizeof (queue)))) Return Null; Q-> Front = Q-> REAR = NULL; Return Q;} BOOL ENQUEUE (Queue * q, ElemType Data) {queuenode * qn = null, * Temp = NULL; if (! q) Return False; if (! (Qn = (QN = (QUEUEUEUE *) Malloc (Sizeof (Queuenode) ))) qn false; Qn-> Data = data; qn-> next = null; if (q-> rest) q-> rest == null q-> rear-> next = Qn; Q-> Rear = qn; if (! q-> front) q-> front = qn; // } Bool Dequeue (Queue * Q, ELMTYPE * DATA) {queenode * qn = null; if (q-> front == null) Return False; Qn = Q-> Front; Q-> front = q- > Front-> Next; * Data = Qn-> Data; Free (QN); Return True;} ELEMTYPE GETFRONTELEM (Queue * Q) {IF (q) Return Q-> Front-> Data;} Bool QueueemPty (Queue * q) {if (! q-> front) Return True; Else Return False;

} Int elementscounts (queue * q) {INT i = 0; queuenode * Temp; Temp = Q-> Front; while (temp) {i ; Temp = Temp-> next;} Return i;} // ==== =================== Void Initmaze1 (Maze Maze [10] [10]) {

For (int i = 1; i <9; i ) {for (int J = 1; j <9; j ) maze [i] [j] .s = 1; printf ("/ n");} maze 2] [3] .S = 0; Maze [2] [4] .S = 0; Maze [2] [5] .S = 0; Maze [3] [5] .S = 0; maze [4] [1] .S = 0; maze [4] [2]. S = 0; Maze [4] [3] .S = 0; Maze [4] [4] .S = 0; Maze [1] [7 ] .S = 0; maze [2] [7] .S = 0; Maze [3] [7] .S = 0; Maze [5] [4] .S = 0; Maze [6] [2]. S = 0; maze [7] [2] .S = 0; Maze [7] [3] .S = 0; Maze [7] [4] .S = 0; Maze [7] [7] .s = 0; Maze [7] [6] .s = 0; Maze [6] [6] .s = 0; //maze[1][5].s=0;} voidin} (Maze Maze [] [10 ]) {INT I, J; Printf ("/ n"); for (i = 0; i <10; i ) {for (j = 0; j <10; j ) {maze [i] [j]. Visited = false; maze [i] [j] .pre.x = 0; maze [i] [j] .pre.y = 0;} printf ("/ n");}} void printmaze (Maze Maze " [10]) {INT I, J; Printf ("/ n"); for (i = 0; i <10; i ) {for (j = 0; j <10; j ) {printf ("% d" , Maze [i] [j] .s);} printf ("/ n");}} // ======================== Void PrintPass (Maze Maze [], int X0, int y0, int x, y; x = x1; y = y1; while (((x1 == x0) && (Y1 = = Y0))) {Printf ("(% D,% d)", x1, y1); x = maze [x1] [y1] .pre.x; y = maze [x1] [y1] .pre.y ; X1 = x; y1 = y; // system ("pause"); } Printf ("(% D,% D)", X0, Y0); Printf ("/ n");} // ========================================================================================================================================== # 12-30-2004 ========= Bool Travelmaze (Maze Maze [] [10], INT X0, INT Y0, INT X1, INT Y1) {Queue * q = initQueue (); Mazepos POS, Tempos; if (! q) EXIT (1);

IF (Maze [x0] .s == 0) Return False; pos.x = x0; pos.y = Y0; MAZE [X0] [Y0] .pre.x = x0; maze [x0] [Y0 ] .pre.y = y0; maze [x0] [y0] .visited = true; enqueue (q, pos); while (! queueempty (q) &&! maze [x1] [y1] .visited) {Tempos = Getfrontelem (q); x0 = Tempos.x; Y0 = Tempos.y; if (Maze [x0 1] [Y0] .s == 1 &&! Maze [x0 1] [Y0] .visited) {x0 ; POS .x = X0; POS.Y = Y0; Enqueue (q, pos); maze [x0] [y0] .visited = true; maze [x0] [y0] .pre.x = x0-1; maze [x0] [Y0] .pre.y = y0;} x0 = Tempos.x; Y0 = Tempos.y; if (Maze [x0-1] [Y0] .s == 1 &&! maze [x0-1] [Y0] .visited) {x0 -; pos.x = x0; pos.y = Y0; enqueue (q, pos); maze [x0] [y0] .visited = true; maze [x0] [y0] .pre.x = x0 1; maze [x0] [y0] .pre.y = Y0;} x0 = Tempos.x; y0 = Tempos.y; if (Maze [x0] [Y0 1] .s == 1 &&! Maze [x0] [Y0 1] .visited) {y0 ; pos.x = x0; pOS.Y = Y0; Enqueue (q, pos); maze [x0] [y0] .visited = true; maze [x0] [Y0] .pre.x = x0; maze [x0] [y0] .pre.y = y0-1; X0 = Tempos.x; Y0 = Tempos.y; IF (Maze [x0] [Y0-1] .s == 1 &&! Maze [x0] [y0-1] .visited) {y0 -; pos.x = X0; POS.Y = Y0; enqueue (q, pos); maze [x0] [y0] .visited = true; maze [x0] [y0] .pre.x = x0; maze [x0] [y0]. pre.y = Y0 1;} Dequeue (q, & pos);} if (Maze [x1] [Y1] .visited) {PrintPass (Maze, 1, 1, 8, 8); Return True;} else return false ;

转载请注明原文地址:https://www.9cbs.com/read-17660.html

New Post(0)