The program automatically generates a maze.

Source: Internet
Author: User

 

The algorithm is as follows:

First, the maze is initialized to all the walls.

Then randomly select from the middle point,

Start recursion, and randomly select the direction to try to move. If the wall is not connected to other channels, set the wall as a path.

Use the depth-first method to continue recursion from the new vertex. If all the surrounding nodes cannot pass through, roll back to the last node and select another direction.

This recursion continues until all vertices are fully explored. The final result is as follows:

 

After studying other people's algorithms, we first assume that there are separated points on the map, and then we can get through these points,

As long as this point is isolated, it can be connected to other points. Such an algorithm will look better and the processing will be much simpler,

The generated images do not have small pieces.

Share my source code as usual: http://www.bkjia.com/uploadfile/2011/1118/20111118024909417.rar

 

The algorithms of others are much more refined, but the code style is really depressing.

// Author: Tanky Woo
// Blog: www.WuTianQi.com
// Brief: a Maze program
# Include <iostream>
# Include <ctime>
# Include <cstdlib>
Using namespace std;
# Define MAZE_MAX 50
Char map [MAZE_MAX + 2] [MAZE_MAX + 2];
Const int x = 11, y = 11;
Int z1, z2;
 
Void printMaze ();
Void makeMaze ();
Int searchPath (int, int );
Int main ()
{
For (int I = 0; I <= x * 2 + 2; ++ I)
For (int j = 0; j <= y * 2 + 2; ++ j)
Map [I] [j] = 1;
 
MakeMaze ();
Cout <"Tanky Woo" <endl;
PrintMaze ();
}
 
Void printMaze ()
{
For (z2 = 1; z2 <= y * 2 + 1; z2 ++)
{
For (z1 = 1; z1 <= x * 2 + 1; z1 ++)
Fputs (map [z2] [z1] = 0? "": "Logging", stdout );
Putchar (10 );
}
Cout <endl;
}
 
Void makeMaze ()
{
For (z1 = 0, z2 = 2 * y + 2; z1 <= 2 * x + 2; ++ z1)
{
Map [z1] [0] = 0;
Map [z1] [z2] = 0;
}
For (z1 = 0, z2 = 2 * x + 2; z1 <= 2 * y + 2; ++ z1)
{
Map [0] [z1] = 0;
Map [z2] [z1] = 0;
}
Map [2] [1] = 0;
Map [2 * x] [2 * y + 1] = 0;
 
Srand (unsigned) time (NULL ));
SearchPath (rand () % x + 1, rand () % y + 1 );
}
 
Int searchPath (int x, int y)
{
Static int dir [4] [2] = {0, 1, 1, 0, 0,-1,-1, 0 };
Int zx = x * 2;
Int zy = y * 2;
Int next, turn, I;
Map [zx] [zy] = 0;
Turn = rand () % 2? 1: 3;
For (I = 0, next = rand () % 4; I <4; ++ I, next = (next + turn) % 4)
If (map [zx + 2 * dir [next] [0] [zy + 2 * dir [next] [1] = 1)
{
Map [zx + dir [next] [0] [zy + dir [next] [1] = 0;
SearchPath (x + dir [next] [0], y + dir [next] [1]);
}
Return 0;
}

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.