Original article address:Irregular Maze generation algorithm 2Author:Girl
First, an intuitive example is provided:
________
╲ _/╲
_ Items _/
/
╱ _/╲ _ ╲/╲ _ ╱ _/
/Users _/users/_ others _/_ others
╱ _/╲ _ ╱ _/
/Users _ accounts
Your _ accounts/
/
╱/╲ _ ╱ _ ╲/╲ _/
/Users _/users/accounts/_/users
╲/╲ _ ╱ ╲ _/╲ _ ╲/
/_ Your _/your
╱/╲ _ ╱ _/╲ _ ╲/
/╲ /_/
╲/╲ _ ╲ /_/
/Users _ accounts _/users
Yao/He
/
╲ _/╲ _
The above honeycomb maze seems to have jumped out of the normal rectangular frame,
But in fact, we can still convert it so that we can almost continue to apply the rectangle generation method.
First, the data structure still uses a two-dimensional array, but the representation needs to be changed:
0 0 0 0 0 0...
0 0 0 0 0 0...
0 0x0x0 x
0 0 0x0x0
0 0x0x0 x
0 0 0x0x0
0 0x0x0 x
...
Each X corresponds to a hexagonal structure with a value of 7 valid bits, 1 access bits, and 6 connection bits,
It is connected to any adjacent part, and the position of the flag is 1 (the corresponding location of the X connected to it must also be marked)
The boundary of the valid data matrix is 2 to ensure that the access is not out of bounds and the access flag is located at 1.
The following is not much different from the general Maze generation algorithm.
The complete generated code is as follows:
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <time. h>
Int yzfy [2, 999] [999];
Int dxy [] [2] = {-1,-1}, {-2, 0}, {-1, 1}, {1, 1}, {2, 0 },{ 1,-1 }};
Int DFS (INT y, int X)
{
If (yzfy [y] [x]) return 0;
Yzfy [y] [x] | = 1;
Int d = rand () & 1? ;
For (int f = rand () % 6, I = 0; I <6; ++ I, F = (F + d) % 6)
{
If (DFS (Y + dxy [f] [0], x + dxy [f] [1])
{
Yzfy [y] [x] | = 2 <F;
Yzfy [Y + dxy [f] [0] [x + dxy [f] [1] | = 2 <(F + 3) % 6 );
}
}
Return 1;
}
Void Gen (int w, int H)
{
Int RW = W * 2 + 3, RH = H * 2 + 3;
Memset (yzfy, 0, sizeof (yzfy ));
For (INT y = 0; y <RH; ++ y)
{
Yzfy [y] [0] = yzfy [y] [1] = 1;
Yzfy [y] [rw-1] = yzfy [y] [rw-2] = 1;
}
For (INT x = 0; x <RW; ++ X)
{
Yzfy [0] [x] = yzfy [1] [x] = 11;
Yzfy [rh-1] [x] = yzfy [rh-2] [x] = 1;
}
Srand (Time (null ));
DFS (RAND () % (W-1) * 2 + 2, Rand () % (h-1) * 2 + 2 );
Yzfy [2] [2] | = 2;
Yzfy [rh-2] [rw-2] | = 2;
}
Int main (INT argc, char * argv [])
{
Int W = 8, H = 10, Y, X;
Int RW = W * 2 + 3, RH = H * 2 + 3;
Gen (W, H );
For (y = 1; y <rh-1; ++ y)
{
If (Y & 1)
{
For (x = 2; x <rw-2; x + = 2, printf (""))
{
Printf (yzfy [y] [x-1]> 3) & 1? "": "Success ");
Printf (yzfy [Y + 1] [x]> 2) & 1? "":"_");
Printf (yzfy [y] [x + 1]> 1) & 1? ");
}
}
Else
{
For (x = 2; x <rw-2; x + = 2)
{
If (x> 2) printf (yzfy [Y + 1] [x-1]> 2) & 1? "":"_");
Printf (yzfy [y] [x] & 2? ");
Printf ("");
Printf (yzfy [y] [x]> 3) & 1? "": "Success ");
}
}
Puts ("");
}
Return 0;
}