patterncMinor
Maze generation, long random time
Viewed 0 times
randommazelongtimegeneration
Problem
How can I improve this code ? It takes an infinite time if i want to create un big maze
(arguments 5000 5000 100 50 100)
main.c
maze.c
The most important file, algorithm is here:
```
#include
#include
#include
#include "maze.h"
#include "deadend.h"
#define visitCell(x, y) map[x][y].visited = 1;
/ take a random cell wich is not visited /
void visitRand(int px, int py, int maxx, int maxy, struct cell **map)
{
int x, y;
do {
x = rand()%(maxx-1);
y = rand()%(maxy-1);
} while (map[x][y].visited != 0);
visitCell(x, y);
*px = x;
*py = y;
}
/ take a random cell wich is already visited /
void visitRandVisited(int px, int py, int maxx, int maxy, struct cell **map)
{
int x, y;
do {
x = rand()%(maxx-1);
y = rand()%(maxy-1);
} while (map[x][y].visited != 1);
*px = x;
*py = y;
}
/ test if all the cells are visited /
int testEndMaze(int maxx, int maxy, struct cell **map) {
int x, y;
for (y = 0; y = 3);
}
/* calcul the percenta
(arguments 5000 5000 100 50 100)
main.c
#include
#include
#include
#include "maze.h"
/*test: valgrind --show-reachable=yes --leak-check=full -v ./dungeon 50 50 100 50 100*/
int main(int argc, char *argv[])
{
int maxx;
int maxy;
int randomness;
int sparseness;
int deadendremoval;
if (argc != 6) {
fprintf(stderr, "Five arguments needed => X, Y, randomness, sparseness, dead-end-removal\n");
return EXIT_FAILURE;
}
maxx = atoi(argv[1]);
maxy = atoi(argv[2]);
randomness = atoi(argv[3]);
sparseness = atoi(argv[4]);
deadendremoval = atoi(argv[5]);
if (randomness 100) {
fprintf(stderr, "Randomness must be between 0 and 100.\n");
return EXIT_FAILURE;
}
if (sparseness 100) {
fprintf(stderr, "Sparseness must be between 0 and 100.\n");
return EXIT_FAILURE;
}
if (deadendremoval 100) {
fprintf(stderr, "Dead-end-removal must be between 0 and 100.\n");
return EXIT_FAILURE;
}
createmaze(maxx, maxy, randomness, sparseness, deadendremoval);
return EXIT_SUCCESS;
}maze.c
The most important file, algorithm is here:
```
#include
#include
#include
#include "maze.h"
#include "deadend.h"
#define visitCell(x, y) map[x][y].visited = 1;
/ take a random cell wich is not visited /
void visitRand(int px, int py, int maxx, int maxy, struct cell **map)
{
int x, y;
do {
x = rand()%(maxx-1);
y = rand()%(maxy-1);
} while (map[x][y].visited != 0);
visitCell(x, y);
*px = x;
*py = y;
}
/ take a random cell wich is already visited /
void visitRandVisited(int px, int py, int maxx, int maxy, struct cell **map)
{
int x, y;
do {
x = rand()%(maxx-1);
y = rand()%(maxy-1);
} while (map[x][y].visited != 1);
*px = x;
*py = y;
}
/ test if all the cells are visited /
int testEndMaze(int maxx, int maxy, struct cell **map) {
int x, y;
for (y = 0; y = 3);
}
/* calcul the percenta
Solution
I didn't look in great detail because there's no explanation of the algorithm you are using, but I did notice that you have several code blocks similar to the following:
That code runs quickly when the map is mostly unvisited, but as the percentage of the map that has been visited increases, thean increasing number of iterations is required to locate an unvisited spot.
If your map is 1000 by 1000, there are 1,000,000 cells. When you get down to the last 10 cells, you have a 1 in 100,000 chance of locating an empty cell with each guess. You should devise a method of tracking used and unused cells separately so you can make a random pick in a single step.
Edit:
testEndMaze() may be another source of inefficiency. Instead of searching every cell to see if it has been visited, can you keep count of cells as you visit them? If so, then testEndMaze() is just comparing your current count of visited cells with the total number of cells in the maze.
do {
x = rand()%(maxx-1);
y = rand()%(maxy-1);
} while (map[x][y].visited != 0);That code runs quickly when the map is mostly unvisited, but as the percentage of the map that has been visited increases, thean increasing number of iterations is required to locate an unvisited spot.
If your map is 1000 by 1000, there are 1,000,000 cells. When you get down to the last 10 cells, you have a 1 in 100,000 chance of locating an empty cell with each guess. You should devise a method of tracking used and unused cells separately so you can make a random pick in a single step.
Edit:
testEndMaze() may be another source of inefficiency. Instead of searching every cell to see if it has been visited, can you keep count of cells as you visit them? If so, then testEndMaze() is just comparing your current count of visited cells with the total number of cells in the maze.
Code Snippets
do {
x = rand()%(maxx-1);
y = rand()%(maxy-1);
} while (map[x][y].visited != 0);Context
StackExchange Code Review Q#3423, answer score: 6
Revisions (0)
No revisions yet.