[Queue Application 1] Random maze | Random start end point * shortest path algorithm, shortest queue

Source: Internet
Author: User
Tags random seed

[Queue Application 1] Random maze | Random start end point * shortest path algorithm, shortest queue

1 # include <iostream> 2 # include <queue> 3 # include <windows. h> 4 # include <time. h> 5 using namespace std; 6 struct position // position 7 {8 int row; 9 int col; 10}; 11 void display (int size, int ** grid ); 12 13 int main () 14 {15 16/***************************** random MAZE is generated, * int size, i, j, p, q, m, n; 18 int ** grid; 19 SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE ), FOREGROUND_INTENSITY | FOREGROUND_RED | 20 FOREGROUND_GREEN | FOREGROUND_BLUE); // white 21 cout <"please enter the size (edge length) of square! (Size <100) "<endl; 22 cin> size; 23 grid = new int * [size + 2]; // dynamically create a two-dimensional array 24 for (I = 0; I <size + 2; I ++) 25 {26 grid [I] = new int [size + 2]; // each pointer element of this pointer array points to another array. 27} 28 for (I = 0; I <size + 2; I ++) 29 {30 grid [I] [0] = 'y '; grid [0] [I] = 'y'; 31 grid [size + 1] [I] = 'y '; grid [I] [size + 1] = 'y'; 32} 33 srand (unsigned) time (NULL )); // Random Seed 34 for (I = 1; I <= size; I ++) 35 {36 for (j = 1; j <= size; j ++) 37 {38 39 if (1 = rand () % 10) // 10% percent achieved 40 grid [I] [j] = 'y '; 41 else 42 grid [I] [j] = 0; 43} 44} 45 p = rand () % size + 1; 46 q = rand () % size + 1; // random start point and End Point 47 grid [p] [q] = 'G'; 48 m = rand () % size + 1; 49 n = Rand () % size + 1; 50 grid [m] [n] = 'R'; 51 52 display (size, grid ); 53/*********************************** find the path *** * **************************** 54. All nbrs are pushed to the queue, find the nbr one by one and press it in again, until the end point is ********************/55 position offset [4]; // direction 56 offset [0]. row = 1; offset [0]. col = 0; // right 57 offset [1]. row = 0; offset [1]. col =-1; // down 58 offset [2]. row =-1; offset [2]. col = 0; // left 59 offset [3]. row = 0; offset [3]. col = 1; // up 60 61 po Sition here; 62 position nbr; 63 position finish; 64 queue <position> Q; 65 66 // initialize 67 here. row = p; here. col = q; 68 finish. row = m; finish. col = n; 69 grid [here. row] [here. col] = 0; 70 while (true) {71 for (I = 0; I <4; I ++) 72 {73 nbr. row = here. row + offset [I]. row; 74 nbr. col = here. col + offset [I]. col; 75 if (nbr. row = finish. row) & (nbr. col = finish. col) 76 {grid [finish. row] [finish. col] = grid [here. row] [here. col] + 1; 77 B Reak; 78} 79 else if (grid [nbr. row] [nbr. col] = 0) 80 {grid [nbr. row] [nbr. col] = grid [here. row] [here. col] + 1; 81 Q. push (nbr);} // All eligible nbrs are pushed to 82 83} 84 if (nbr. row = finish. row) & (nbr. col = finish. col) 85 {grid [finish. row] [finish. col] = grid [here. row] [here. col] + 1; 86 break; 87} 88 if (Q. empty () 89 {90 cout <"no path! "<Endl; 91 break; 92} 93 here = Q. front (); // retrieve the front 94 Q. pop (); 95 96 }; 97 ******* * ******** 98, the difference between each loop and the end count is ****************/99 here = finish; 100 int length = grid [finish. row] [finish. col]; 101 for (j = 1; j <length; j ++) 102 {103 for (I = 0; I <4; I ++) 104 {105 nbr. row = here. row + offset [I]. row; 106 nbr. col = here. col + offset [I]. col; 107 if (grid [nbr. row] [nbr. col] = (grid [finish. ro W] [finish. col]-j) 108 {109 grid [nbr. row] [nbr. col] = 'P'; // you have changed the value to the next loop into p-1110 here = nbr; 111 break; 112} 113} 114} 115/*********************** display ********* * **********************/116 SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE ), BACKGROUND_INTENSITY | BACKGROUND_RED | 117 BACKGROUND_GREEN | BACKGROUND_BLUE); // white 118 cout <"Show you the shortest path! "<Endl; 119 grid [p] [q] = 'G'; 120 grid [m] [n] = 'R'; 121 display (size, grid ); 122 return 0; 123} 124 125 ************ * *************************/126 void display (int size, int ** grid) 127 {int I, j; 128 for (I = 0; I <size + 2; I ++) 129 {130 for (j = 0; j <size + 2; j ++) 131 {132 if (grid [I] [j] = 'y') 133 {134 SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE ), BACKGROUND_INTENSITY | BACKGROUND_RED | 135 BACKGROUND_GREEN); // The Yellow 136 cout <''; 137} 138 139 else if (grid [I] [j] = 'G') 140 {141 SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY | 142 BACKGROUND_GREEN ); // green 143 cout <<'' <''; 144} 145 else if (grid [I] [j] = 'R ') 146 {147 SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY | 148 BACKGROUND_RED); // red 149 cout <<'' <''; 150} 151 else if (grid [I] [j] = 'P') 152 {153 SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY | 154 BACKGROUND_GREEN | BACKGROUND_BLUE ); 155 cout <''; 156} 157 else158 {SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY | BACKGROUND_RED | 159 BACKGROUND_GREEN | BACKGROUND_BLUE ); // white 160 cout <<'' <''; 161} 162 163} 164 cout <endl; 165} 166}

 

L time is the seed. The white lattice has a probability of 10%. The coordinates of the green and red blocks are randomly generated.

Srand (unsigned) time (NULL); // use time as Random Seed

For (I = 1; I <= size; I ++)

{

For (j = 1; j <= size; j ++)

{

If (1 = rand () % 10) // 10% percent

Grid [I] [j] = 'y ';

Else

Grid [I] [j] = 0;

}

}

P = rand () % size + 1;

Q = rand () % size + 1; // random start point and end point

Grid [p] [q] = 'G'; // green

M = rand () % size + 1;

N = rand () % size + 1;

Grid [m] [n] = 'R'; // red

L Queue: All qualified nbrs are pushed into the Queue. Each time a new here appears, the nbr is searched for until the end point is found. If the queue is empty, no path is output! And jumps out. (For details, see the program)

L color settings use the function in the S. h header file to set the background color of the square. See another article.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.