-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv4.c
274 lines (234 loc) · 6.73 KB
/
v4.c
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <stdio.h>
#include <stdlib.h>
// ---------------- Linked List for Explored Path ----------------
typedef struct Node {
int x, y;
struct Node* next;
} Node;
Node* createNode(int x, int y) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->x = x;
newNode->y = y;
newNode->next = NULL;
return newNode;
}
// ---------------- Stack for Backtracking ----------------
typedef struct Stack {
int x, y;
struct Stack* next;
} Stack;
Stack* top = NULL;
void push(int x, int y) {
Stack* newNode = (Stack*)malloc(sizeof(Stack));
newNode->x = x;
newNode->y = y;
newNode->next = top;
top = newNode;
}
void pop(int* x, int* y) {
if (top == NULL) return;
Stack* temp = top;
*x = top->x;
*y = top->y;
top = top->next;
free(temp);
}
// ---------------- Queue for BFS ----------------
typedef struct Point {
int x, y;
} Point;
typedef struct Queue {
int front, rear, size;
int capacity;
Point* array;
} Queue;
Queue* createQueue(int capacity) {
Queue* queue = (Queue*)malloc(sizeof(Queue));
queue->capacity = capacity;
queue->front = queue->size = 0;
queue->rear = capacity - 1;
queue->array = (Point*)malloc(queue->capacity * sizeof(Point));
return queue;
}
int isFull(Queue* queue) {
return (queue->size == queue->capacity);
}
int isEmpty(Queue* queue) {
return (queue->size == 0);
}
void enqueue(Queue* queue, int x, int y) {
if (isFull(queue)) return;
queue->rear = (queue->rear + 1) % queue->capacity;
queue->array[queue->rear].x = x;
queue->array[queue->rear].y = y;
queue->size++;
}
Point dequeue(Queue* queue) {
Point p = {-1, -1}; // Default value
if (isEmpty(queue)) return p;
p = queue->array[queue->front];
queue->front = (queue->front + 1) % queue->capacity;
queue->size--;
return p;
}
// ---------------- Maze and Helper Structures ----------------
int rows, cols; // Maze dimensions
int** maze; // Dynamic maze
int** visited; // Dynamic visited array
int dx[] = {0, 0, -1, 1}; // Directions for movement
int dy[] = {-1, 1, 0, 0};
// ---------------- Function Prototypes ----------------
void displayMaze();
int isSafe(int x, int y);
void bfsSolveMaze();
void findAllPaths(int x, int y, Node* path);
void resetVisited();
void freePath(Node* path);
// ---------------- Utility Functions ----------------
void displayMaze() {
printf("Maze:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", maze[i][j]);
}
printf("\n");
}
printf("\n");
}
int isSafe(int x, int y) {
return (x >= 0 && x < rows && y >= 0 && y < cols && maze[x][y] == 0 && !visited[x][y]);
}
void resetVisited() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
visited[i][j] = 0;
}
}
}
void freePath(Node* path) {
Node* temp;
while (path) {
temp = path;
path = path->next;
free(temp);
}
}
// ---------------- BFS SolveMaze ----------------
void bfsSolveMaze() {
Queue* queue = createQueue(rows * cols);
enqueue(queue, 0, 0);
visited[0][0] = 1;
// Parent array to reconstruct the path
Point parent[rows][cols];
parent[0][0] = (Point){-1, -1}; // Start point has no parent
while (!isEmpty(queue)) {
Point p = dequeue(queue);
int x = p.x;
int y = p.y;
// Check if we reached the end
if (x == rows - 1 && y == cols - 1) {
// Reconstruct the path
Node* path = NULL;
int currX = x, currY = y;
while (currX != -1 && currY != -1) {
Node* newNode = createNode(currX, currY);
newNode->next = path;
path = newNode;
Point prev = parent[currX][currY];
currX = prev.x;
currY = prev.y;
}
// Display the path
printf("Shortest Path using BFS:\n");
Node* temp = path;
while (temp) {
printf("(%d, %d) -> ", temp->x, temp->y);
temp = temp->next;
}
printf("END\n");
// Free the path
freePath(path);
return;
}
// Explore neighbors
for (int i = 0; i < 4; i++) {
int newX = x + dx[i];
int newY = y + dy[i];
if (isSafe(newX, newY)) {
visited[newX][newY] = 1;
enqueue(queue, newX, newY);
parent[newX][newY] = (Point){x, y}; // Set the parent
}
}
}
printf("No path found using BFS.\n");
}
// ---------------- Find All Paths ----------------
void findAllPaths(int x, int y, Node* path) {
if (x == rows - 1 && y == cols - 1) {
// Print the current path
Node* temp = path;
printf("Path: ");
while (temp) {
printf("(%d, %d) -> ", temp->x, temp->y);
temp = temp->next;
}
printf("END\n");
return;
}
// Explore neighbors
for (int i = 0; i < 4; i++) {
int newX = x + dx[i];
int newY = y + dy[i];
if (isSafe(newX, newY)) {
visited[newX][newY] = 1;
// Add to path
Node* newPathNode = createNode(newX, newY);
newPathNode->next = path;
findAllPaths(newX, newY, newPathNode);
// Backtrack
visited[newX][newY] = 0;
free(newPathNode);
}
}
}
// ---------------- Main Function ----------------
int main() {
// Input maze dimensions
printf("Enter number of rows in the maze: ");
scanf("%d", &rows);
printf("Enter number of columns in the maze: ");
scanf("%d", &cols);
// Allocate memory for maze and visited arrays
maze = (int*)malloc(rows * sizeof(int));
visited = (int*)malloc(rows * sizeof(int));
for (int i = 0; i < rows; i++) {
maze[i] = (int*)malloc(cols * sizeof(int));
visited[i] = (int*)malloc(cols * sizeof(int));
}
// Input maze configuration
printf("Enter the maze row by row (0 for path, 1 for wall):\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &maze[i][j]);
}
}
displayMaze();
resetVisited();
printf("Finding all paths:\n");
Node* initialPath = createNode(0, 0); // Start from (0, 0)
visited[0][0] = 1; // Mark the starting point as visited
findAllPaths(0, 0, initialPath);
freePath(initialPath);
resetVisited();
printf("Finding shortest path using BFS:\n");
bfsSolveMaze();
// Free allocated memory
for (int i = 0; i < rows; i++) {
free(maze[i]);
free(visited[i]);
}
free(maze);
free(visited);
return 0;
}