fork download
  1. type maze_block = {
  2. walkable : bool;
  3. is_finish : bool;
  4. marked : bool;
  5. prev_coordinate : int * int
  6. }
  7.  
  8. let is_in_bounds (x, y) (xbound, ybound) = (x >= 0) && (x < xbound) && (y >= 0) && (y < ybound)
  9.  
  10. let dimensions (grid: 'a array array) = (Array.length grid, Array.length grid.(0))
  11.  
  12. let access_grid (grid: 'a array array) (x, y) = grid.(x).(y)
  13.  
  14. let mutate_grid (grid: 'a array array) (x, y) (new_elem: 'a) = begin grid.(x).(y) <- new_elem; () end
  15.  
  16. let mark_for_visitation currentXY neighborXY (grid: maze_block array array) (q: (int * int) Queue.t) =
  17. let xybounds = dimensions grid in
  18. if not (is_in_bounds neighborXY xybounds) then ()
  19. else let neighbor_block = access_grid grid neighborXY in
  20. if neighbor_block.marked || (not neighbor_block.walkable) then ()
  21. else
  22. begin
  23. Queue.add neighborXY q;
  24. mutate_grid grid neighborXY {neighbor_block with marked = true; prev_coordinate = currentXY}
  25. end
  26.  
  27.  
  28. let mark_all_4directions (currentX, currentY) (grid: maze_block array array) (q: (int * int) Queue.t) =
  29. begin
  30. mark_for_visitation (currentX, currentY) (currentX + 1, currentY) grid q;
  31. mark_for_visitation (currentX, currentY) (currentX - 1, currentY) grid q;
  32. mark_for_visitation (currentX, currentY) (currentX, currentY - 1) grid q;
  33. mark_for_visitation (currentX, currentY) (currentX, currentY + 1) grid q
  34. end
  35.  
  36. let assemble_trail (maze_grid: maze_block array array) currentXY =
  37. let rec assemble_helper (grid: maze_block array array) (thisX, thisY) trailList = match (thisX, thisY) with
  38. | (-1, -1) -> trailList
  39. | _ -> assemble_helper grid (grid.(thisX).(thisY).prev_coordinate) ((thisX, thisY) :: trailList)
  40. in
  41. assemble_helper maze_grid currentXY []
  42.  
  43. let rec maze_solver_loop (maze_grid: maze_block array array) (q: (int * int) Queue.t) =
  44. if Queue.is_empty q then [] else
  45. let currentXY = (Queue.take q) in
  46. let current_block = access_grid maze_grid currentXY in
  47. if current_block.is_finish then assemble_trail maze_grid currentXY
  48. else
  49. begin
  50. mark_all_4directions currentXY maze_grid q;
  51. maze_solver_loop maze_grid q
  52. end
  53.  
  54. let solve_maze startXY (maze_grid: maze_block array array) =
  55. let xybounds = dimensions maze_grid in
  56. let start_block = access_grid maze_grid startXY in
  57. if (not (is_in_bounds startXY xybounds) || not start_block.walkable)
  58. then []
  59. else let new_queue = Queue.create () in
  60. begin
  61. Queue.add startXY new_queue;
  62. mutate_grid maze_grid startXY {start_block with marked = true};
  63. maze_solver_loop maze_grid new_queue
  64. end
  65.  
  66. let convert_to_maze (num_array: int array array) : maze_block array array =
  67. Array.map (fun arr -> (Array.map (fun i -> {walkable = (i == 0); is_finish = false; marked = false; prev_coordinate = (-1, -1)}) arr)) num_array
  68.  
  69. let solve_this_maze startXY finishXY num_array =
  70. let xybounds = dimensions num_array in
  71. let maze = convert_to_maze num_array in
  72. let finish_in_bounds = is_in_bounds finishXY xybounds in
  73. if not finish_in_bounds then []
  74. else
  75. let finish_block = access_grid maze finishXY in
  76. let finish_reachable = finish_block.walkable in
  77. if finish_reachable then
  78. begin
  79. mutate_grid maze finishXY {finish_block with is_finish = true};
  80. solve_maze startXY maze
  81. end
  82. else []
  83.  
  84. let print_tuple_list tuple_list =
  85. let print_tuple (x, y) = print_endline ((string_of_int x) ^ ", " ^ (string_of_int y))
  86. in List.iter (fun x -> print_tuple x) tuple_list
  87.  
  88. ;;
  89.  
  90. (*
  91.  
  92. how to test run:
  93.  
  94. start ocaml REPL
  95.  
  96. #use "WhateverFolder/mazeSolver2.ml";;
  97.  
  98. let arr = [|[|0;0;0;0;0|]; [|1;1;1;1;0|]; [|0;0;0;0;0|]; [|0;1;1;1;1|]; [|0;0;0;0;0|]|];;
  99. solve_this_maze (0,0) (4,4) arr;;
  100.  
  101.  
  102. let arr2 = [|[|1;1;1;1;1;1;0|];[|0;0;0;0;0;0;0|];[|1;1;1;1;1;1;0|];[|0;0;0;0;0;0;0|];[|0;1;1;1;1;1;1|];[|0;0;0;0;0;0;0|];[|1;1;1;0;1;1;1|];[|0;0;0;0;0;0;0|];[|0;1;1;1;1;1;0|]|];;
  103. solve_this_maze (1,0) (8,6) arr2;;
  104. *)
  105.  
  106. let arr = [|[|1;1;1;1;1;1;0|];[|0;0;0;0;0;0;0|];[|1;1;1;1;1;1;0|];[|0;0;0;0;0;0;0|];[|0;1;1;1;1;1;1|];[|0;0;0;0;0;0;0|];[|1;1;1;0;1;1;1|];[|0;0;0;0;0;0;0|];[|0;1;1;1;1;1;0|]|] in
  107. print_tuple_list (solve_this_maze (1,0) (8,6) arr);
  108.  
  109.  
  110. print_tuple_list (solve_this_maze (0,0) (15,15) (Array.make_matrix 16 16 0));
  111.  
  112.  
  113. print_tuple_list (solve_this_maze (0,0) (31,31) (Array.make_matrix 32 32 0));
  114.  
  115.  
  116. print_tuple_list (solve_this_maze (0,0) (255,255) (Array.make_matrix 256 256 0));
Success #stdin #stdout 0.24s 6456KB
stdin
Standard input is empty
stdout
1, 0
1, 1
1, 2
1, 3
1, 4
1, 5
1, 6
2, 6
3, 6
3, 5
3, 4
3, 3
3, 2
3, 1
3, 0
4, 0
5, 0
5, 1
5, 2
5, 3
6, 3
7, 3
7, 4
7, 5
7, 6
8, 6

0, 0
1, 0
2, 0
3, 0
4, 0
5, 0
6, 0
7, 0
8, 0
9, 0
10, 0
11, 0
12, 0
13, 0
14, 0
15, 0
15, 1
15, 2
15, 3
15, 4
15, 5
15, 6
15, 7
15, 8
15, 9
15, 10
15, 11
15, 12
15, 13
15, 14
15, 15

0, 0
1, 0
2, 0
3, 0
4, 0
5, 0
6, 0
7, 0
8, 0
9, 0
10, 0
11, 0
12, 0
13, 0
14, 0
15, 0
16, 0
17, 0
18, 0
19, 0
20, 0
21, 0
22, 0
23, 0
24, 0
25, 0
26, 0
27, 0
28, 0
29, 0
30, 0
31, 0
31, 1
31, 2
31, 3
31, 4
31, 5
31, 6
31, 7
31, 8
31, 9
31, 10
31, 11
31, 12
31, 13
31, 14
31, 15
31, 16
31, 17
31, 18
31, 19
31, 20
31, 21
31, 22
31, 23
31, 24
31, 25
31, 26
31, 27
31, 28
31, 29
31, 30
31, 31

0, 0
1, 0
2, 0
3, 0
4, 0
5, 0
6, 0
7, 0
8, 0
9, 0
10, 0
11, 0
12, 0
13, 0
14, 0
15, 0
16, 0
17, 0
18, 0
19, 0
20, 0
21, 0
22, 0
23, 0
24, 0
25, 0
26, 0
27, 0
28, 0
29, 0
30, 0
31, 0
32, 0
33, 0
34, 0
35, 0
36, 0
37, 0
38, 0
39, 0
40, 0
41, 0
42, 0
43, 0
44, 0
45, 0
46, 0
47, 0
48, 0
49, 0
50, 0
51, 0
52, 0
53, 0
54, 0
55, 0
56, 0
57, 0
58, 0
59, 0
60, 0
61, 0
62, 0
63, 0
64, 0
65, 0
66, 0
67, 0
68, 0
69, 0
70, 0
71, 0
72, 0
73, 0
74, 0
75, 0
76, 0
77, 0
78, 0
79, 0
80, 0
81, 0
82, 0
83, 0
84, 0
85, 0
86, 0
87, 0
88, 0
89, 0
90, 0
91, 0
92, 0
93, 0
94, 0
95, 0
96, 0
97, 0
98, 0
99, 0
100, 0
101, 0
102, 0
103, 0
104, 0
105, 0
106, 0
107, 0
108, 0
109, 0
110, 0
111, 0
112, 0
113, 0
114, 0
115, 0
116, 0
117, 0
118, 0
119, 0
120, 0
121, 0
122, 0
123, 0
124, 0
125, 0
126, 0
127, 0
128, 0
129, 0
130, 0
131, 0
132, 0
133, 0
134, 0
135, 0
136, 0
137, 0
138, 0
139, 0
140, 0
141, 0
142, 0
143, 0
144, 0
145, 0
146, 0
147, 0
148, 0
149, 0
150, 0
151, 0
152, 0
153, 0
154, 0
155, 0
156, 0
157, 0
158, 0
159, 0
160, 0
161, 0
162, 0
163, 0
164, 0
165, 0
166, 0
167, 0
168, 0
169, 0
170, 0
171, 0
172, 0
173, 0
174, 0
175, 0
176, 0
177, 0
178, 0
179, 0
180, 0
181, 0
182, 0
183, 0
184, 0
185, 0
186, 0
187, 0
188, 0
189, 0
190, 0
191, 0
192, 0
193, 0
194, 0
195, 0
196, 0
197, 0
198, 0
199, 0
200, 0
201, 0
202, 0
203, 0
204, 0
205, 0
206, 0
207, 0
208, 0
209, 0
210, 0
211, 0
212, 0
213, 0
214, 0
215, 0
216, 0
217, 0
218, 0
219, 0
220, 0
221, 0
222, 0
223, 0
224, 0
225, 0
226, 0
227, 0
228, 0
229, 0
230, 0
231, 0
232, 0
233, 0
234, 0
235, 0
236, 0
237, 0
238, 0
239, 0
240, 0
241, 0
242, 0
243, 0
244, 0
245, 0
246, 0
247, 0
248, 0
249, 0
250, 0
251, 0
252, 0
253, 0
254, 0
255, 0
255, 1
255, 2
255, 3
255, 4
255, 5
255, 6
255, 7
255, 8
255, 9
255, 10
255, 11
255, 12
255, 13
255, 14
255, 15
255, 16
255, 17
255, 18
255, 19
255, 20
255, 21
255, 22
255, 23
255, 24
255, 25
255, 26
255, 27
255, 28
255, 29
255, 30
255, 31
255, 32
255, 33
255, 34
255, 35
255, 36
255, 37
255, 38
255, 39
255, 40
255, 41
255, 42
255, 43
255, 44
255, 45
255, 46
255, 47
255, 48
255, 49
255, 50
255, 51
255, 52
255, 53
255, 54
255, 55
255, 56
255, 57
255, 58
255, 59
255, 60
255, 61
255, 62
255, 63
255, 64
255, 65
255, 66
255, 67
255, 68
255, 69
255, 70
255, 71
255, 72
255, 73
255, 74
255, 75
255, 76
255, 77
255, 78
255, 79
255, 80
255, 81
255, 82
255, 83
255, 84
255, 85
255, 86
255, 87
255, 88
255, 89
255, 90
255, 91
255, 92
255, 93
255, 94
255, 95
255, 96
255, 97
255, 98
255, 99
255, 100
255, 101
255, 102
255, 103
255, 104
255, 105
255, 106
255, 107
255, 108
255, 109
255, 110
255, 111
255, 112
255, 113
255, 114
255, 115
255, 116
255, 117
255, 118
255, 119
255, 120
255, 121
255, 122
255, 123
255, 124
255, 125
255, 126
255, 127
255, 128
255, 129
255, 130
255, 131
255, 132
255, 133
255, 134
255, 135
255, 136
255, 137
255, 138
255, 139
255, 140
255, 141
255, 142
255, 143
255, 144
255, 145
255, 146
255, 147
255, 148
255, 149
255, 150
255, 151
255, 152
255, 153
255, 154
255, 155
255, 156
255, 157
255, 158
255, 159
255, 160
255, 161
255, 162
255, 163
255, 164
255, 165
255, 166
255, 167
255, 168
255, 169
255, 170
255, 171
255, 172
255, 173
255, 174
255, 175
255, 176
255, 177
255, 178
255, 179
255, 180
255, 181
255, 182
255, 183
255, 184
255, 185
255, 186
255, 187
255, 188
255, 189
255, 190
255, 191
255, 192
255, 193
255, 194
255, 195
255, 196
255, 197
255, 198
255, 199
255, 200
255, 201
255, 202
255, 203
255, 204
255, 205
255, 206
255, 207
255, 208
255, 209
255, 210
255, 211
255, 212
255, 213
255, 214
255, 215
255, 216
255, 217
255, 218
255, 219
255, 220
255, 221
255, 222
255, 223
255, 224
255, 225
255, 226
255, 227
255, 228
255, 229
255, 230
255, 231
255, 232
255, 233
255, 234
255, 235
255, 236
255, 237
255, 238
255, 239
255, 240
255, 241
255, 242
255, 243
255, 244
255, 245
255, 246
255, 247
255, 248
255, 249
255, 250
255, 251
255, 252
255, 253
255, 254
255, 255