Looking for the shortest path of circuit wiring (Queue)

xiaoxiao2021-03-05  22

Bool Findpath (position start, position finish, int & pathlen, posing * & path) {// Looking for the shortest path from Start to Finish

IF (Start.Row == Finish.Row) && (Start.col == Finish.col)) {pathlen = 0; return true;}

// Initializing the wall of the surrounding grid (INT i = 0; i <= m 1; i ) {grid [0] [i] = Grid [m 1] [i] = 1; Grid [i] [ 0] = Grid [i] [m 1] = 1;}

// Initialize Offset Position Offset [4]; Offset [0 ].Row = 0; Offset [0] .col = 1; // Right Offset [1] .Row = 1; Offset [1] .col = 0; / / Next Offset [2 ].Row = 0; Offset [2] .col = -1; // Left Offset [3] .row = -1; Offset [3] .col = 0; //

Int numofnbrs = 4; position her, nbr; here.row = start.row; here.col = start.col; grid [here.row] [here.col] = 2; // Block

/ / Mark the reachable grid position LinkedQueue q; do {// tag adjacent position for (INT i = 0; i

IF (q.isempty ()) Return False; Q.Delete (Here);} while (1);

// Structure path pathlen = grid [finish.row] [finish.col] -2; path = new position [Pathlen];

// retract to finish; here = finish; for (int J = pathlen-1; j> = 0; j -) {path [j] = here; // Looking for the previous position for (INT i = 0; i

Return True;}

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

New Post(0)