-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaze.c
More file actions
300 lines (271 loc) · 7.06 KB
/
maze.c
File metadata and controls
300 lines (271 loc) · 7.06 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<time.h>
#ifdef _WIN32
#include <windows.h>
#define usleep(x) Sleep((x)/1000)
#endif
#define NMAX 50
int matrix[NMAX][NMAX];
int n=20;
int queue_size = 0;
// 1为墙,0为通路,2为走过的路,3为死路
int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
char* direction_arrows[] = {"→", "↓", "←", "↑"};
// 终端输出
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void print(int x, int y, char* s, int threads){
// 打印并刷新
printf("\033[%d;%dH%s", x+1, y*2+1, s);
fflush(stdout);
usleep(40000/threads);
}
void init_matrix(){
// 初始化迷宫
if(n>NMAX)
n = NMAX;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
matrix[i][j] = 1;
}
void print_matrix(){
// 打印完整迷宫
clearScreen();
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(matrix[i][j]==1)
printf("██");
else
printf(" ");
fflush(stdout);
usleep(800);
}
printf("\n");
}
}
// 栈的实现
int stack_depth = 0;
int stack[NMAX*NMAX][3];
void stack_push(int x, int y, int direction){
stack[stack_depth][0] = x;
stack[stack_depth][1] = y;
stack[stack_depth][2] = direction;
stack_depth++;
}
void stack_pop(){
print(stack[stack_depth-1][0], stack[stack_depth-1][1], "░░", 1);
stack_depth--;
}
void stack_print(){
int temp_count=0;
for(int i=0;i<stack_depth;i++){
if(temp_count++%5==0)
printf("\n");
printf("(%02d,%02d) -> ", stack[i][0], stack[i][1]);
fflush(stdout);
usleep(40000);
}
printf("\n");
}
// 队列的实现
typedef struct queue{
int x,y;
struct queue* next;
}queue;
queue* head = NULL;
queue* tail = NULL;
void queue_push(int x, int y){
queue* temp = (queue*)malloc(sizeof(queue));
temp->x = x;
temp->y = y;
temp->next = NULL;
if(head==NULL){
head = temp;
tail = temp;
}else{
tail->next = temp;
tail = temp;
}
queue_size++;
}
void queue_pop(int* x, int* y){
if(head==NULL){
*x = -1;
*y = -1;
return;
}
queue* temp = head;
head = head->next;
*x = temp->x;
*y = temp->y;
free(temp);
queue_size--;
}
void refresh(){
if(stack_depth>1)
print(stack[stack_depth-2][0], stack[stack_depth-2][1], direction_arrows[stack[stack_depth-1][2]], 1);
if(stack_depth>0)
print(stack[stack_depth-1][0], stack[stack_depth-1][1], "㊣", 1);
}
int solve_matrix(int x,int y){
// 递归解决迷宫
matrix[x][y] = 2;
refresh();
if(x==n-2 && y==n-1)
return 1;
int random;
int cur = 0;
int arr[4] = {-1,-1,-1,-1};
while(cur!=4){
random = rand()%4;
int i;
for(i=0;i<cur;i++)
if(arr[i]==random)
break;
if(i==cur) arr[cur++] = random;
}
for(int i=0;i<4;i++){
int nx = x + directions[arr[i]][0];
int ny = y + directions[arr[i]][1];
if(nx<0 || nx>=n || ny<0 || ny>=n)
continue;
if(matrix[nx][ny]==0){
stack_push(nx, ny, arr[i]);
if(solve_matrix(nx, ny)) return 1;
stack_pop();
matrix[nx][ny]=3;
refresh();
}
}
return 0;
}
void input_matrix(){
// 手动输入迷宫
clearScreen();
printf("Remember: 1 for blocks and 0 for paths.\nPlease input the size of the matrix: ");
scanf("%d", &n);
init_matrix();
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
scanf("%d", &matrix[i][j]);
usleep(100000);
}
void find_next_step(int now_x, int now_y, int* next_x_ref, int* next_y_ref, int* status){
*status = 1;
int go_directions[4][2] = {{0,-1},{-1,0},{0,1},{1,0}};
int detect_directions[4][2] = {{-1,-1},{-1,1},{1,1},{1,-1}};
int available_directions[4] = {1,1,1,1};
for(int i=0;i<4;i++){
int test_x = now_x + go_directions[i][0];
int test_y = now_y + go_directions[i][1];
if(matrix[test_x][test_y]==0)
available_directions[i]=0;
else if(test_x == 0 || test_x == n-1 || test_y == 0 || test_y == n-1)
available_directions[i]=0;
}
for(int i=0;i<4;i++){
int test_x = now_x + detect_directions[i][0];
int test_y = now_y + detect_directions[i][1];
if(matrix[test_x][test_y]==0){
available_directions[i]=0;
available_directions[(i+1)%4]=0;
}
}
int directions_count = 0;
for(int i=0;i<4;i++)
directions_count += available_directions[i];
if(directions_count == 0) return;
int next_x, next_y;
while(1){
int direction = rand()%4;
while(available_directions[direction]==0)
direction = rand()%4;
next_x = now_x + go_directions[direction][0];
next_y = now_y + go_directions[direction][1];
if(next_x<1 || next_x>=n-1 || next_y<1 || next_y>=n-1){
if(directions_count==1) return;
else continue;
}
break;
}
matrix[next_x][next_y] = 0;
print(next_x, next_y, " ", queue_size);
*next_x_ref = next_x;
*next_y_ref = next_y;
*status = 0;
return;
}
void generate_matrix(){
// 随机生成迷宫
if(n>NMAX)
n = NMAX;
int begin_x = 1, begin_y = 0;
int end_x = n-2, end_y = n-1;
matrix[begin_x][begin_y] = 0;
matrix[end_x][end_y] = 0;
print(begin_x, begin_y, "▒▒", 1);
print(end_x, end_y, "▒▒", 1);
int now_x, now_y;
int next_x, next_y, status;
queue_push(1,0);
queue_push(n-2,n-1);
while(queue_size>0){
int random = rand()%3;
queue_pop(&now_x, &now_y);
if(random==0){
find_next_step(now_x, now_y, &next_x, &next_y, &status);
if(status==0)
queue_push(next_x, next_y);
}
find_next_step(now_x, now_y, &next_x, &next_y, &status);
if(status==0)
queue_push(next_x, next_y);
}
}
int main(){
#ifdef _WIN32
#endif
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
DWORD mode;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleMode(hConsole, &mode);
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hConsole, mode);
#endif
srand(time(NULL));
clearScreen();
printf("Choose a mode.\n1. Manual input\n2. Random input\nYour choice: ");
int type = 0;
scanf("%d", &type);
getchar();
if(type==1){
input_matrix();
print_matrix();
solve_matrix(1,0);
stack_print();
}else if(type==2){
clearScreen();
printf("Please input the size of the matrix: ");
scanf("%d", &n);
init_matrix();
print_matrix();
generate_matrix();
usleep(100000);
solve_matrix(1,0);
print(n+1,0,"\n", 1);
stack_print();
}
printf("按任意键退出...\n");
getchar(); // 等待用户输入
return 0;
}