forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_based_maze_generator_and_solver.c
More file actions
130 lines (111 loc) · 2.49 KB
/
Copy pathtext_based_maze_generator_and_solver.c
File metadata and controls
130 lines (111 loc) · 2.49 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define WALL '#'
#define PATH ' '
#define SOLUTION '.'
typedef struct
{
int row, col;
} Cell;
int rows, cols;
char **maze;
/* Direction vectors: N, S, E, W */
int dr[4] = {-1, 1, 0, 0};
int dc[4] = {0, 0, 1, -1};
/* Initialize maze with all walls */
void initMaze()
{
maze = malloc(rows * sizeof(char *));
for (int i = 0; i < rows; i++)
{
maze[i] = malloc(cols * sizeof(char));
for (int j = 0; j < cols; j++)
maze[i][j] = WALL;
}
}
/* Utility: shuffle directions */
void shuffle(int *arr, int n)
{
for (int i = n - 1; i > 0; i--)
{
int j = rand() % (i + 1);
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
/* DFS Maze generator */
void generateMazeDFS(int r, int c)
{
maze[r][c] = PATH;
int dirs[4] = {0, 1, 2, 3};
shuffle(dirs, 4);
for (int i = 0; i < 4; i++)
{
int nr = r + dr[dirs[i]] * 2;
int nc = c + dc[dirs[i]] * 2;
if (nr > 0 && nr < rows - 1 && nc > 0 && nc < cols - 1 && maze[nr][nc] == WALL)
{
maze[r + dr[dirs[i]]][c + dc[dirs[i]]] = PATH; // remove wall
generateMazeDFS(nr, nc);
}
}
}
/* Print maze */
void printMaze()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
printf("%c", maze[i][j]);
printf("\n");
}
}
/* Solver using DFS */
int solveMaze(int r, int c, int er, int ec)
{
if (r == er && c == ec)
return 1; // reached end
if (maze[r][c] != PATH)
return 0;
maze[r][c] = SOLUTION; // mark path
for (int i = 0; i < 4; i++)
{
int nr = r + dr[i];
int nc = c + dc[i];
if (solveMaze(nr, nc, er, ec))
return 1;
}
maze[r][c] = PATH; // backtrack
return 0;
}
int main()
{
srand(time(NULL));
printf("Enter maze size (rows cols, odd numbers recommended): ");
if (scanf("%d %d", &rows, &cols) != 2)
return 1;
initMaze();
// Generate maze starting from (1,1)
generateMazeDFS(1, 1);
// Set entrance and exit
maze[0][1] = PATH;
maze[rows - 1][cols - 2] = PATH;
printf("\nGenerated Maze:\n");
printMaze();
if (solveMaze(0, 1, rows - 1, cols - 2))
{
printf("\nSolved Maze:\n");
printMaze();
}
else
{
printf("No solution found!\n");
}
// Free memory
for (int i = 0; i < rows; i++)
free(maze[i]);
free(maze);
return 0;
}