1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
| public class MazeAlgorithm
{
Random random = new Random();
public int[,] GenerateMaze(int rows, int cols)
{
int[,] maze = new int[rows, cols];
// Initialize maze with walls
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
maze[i, j] = 1;
}
}
// Set starting point
maze[1, 1] = 0;
DFS(maze, 1, 1);
return maze;
}
void DFS(int[,] maze, int row, int col)
{
int[] directions = { 1, 2, 3, 4 };
Shuffle(directions);
foreach (int dir in directions)
{
int[] dRow = { 0, 0, 1, -1 };
int[] dCol = { 1, -1, 0, 0 };
int newRow = row + 2 * dRow[dir - 1];
int newCol = col + 2 * dCol[dir - 1];
if (newRow > 0 && newRow < maze.GetLength(0) - 1 &&
newCol > 0 && newCol < maze.GetLength(1) - 1 &&
maze[newRow, newCol] == 1)
{
maze[row + dRow[dir - 1], col + dCol[dir - 1]] = 0;
maze[newRow, newCol] = 0;
DFS(maze, newRow, newCol);
}
}
}
void Shuffle(int[] array)
{
int n = array.Length;
for (int i = 0; i < n; i++)
{
int r = i + random.Next(n - i);
int temp = array[r];
array[r] = array[i];
array[i] = temp;
}
}
}
|