-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
130 lines (111 loc) · 3.51 KB
/
Copy pathmain.lua
File metadata and controls
130 lines (111 loc) · 3.51 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
local function readMaze(filename)
local maze = {}
local file = io.open(filename, "r")
if not file then
error("Could not open read file " .. filename)
end
for line in file:lines() do
local row = {}
for char in line:gmatch(".") do
table.insert(row, char)
end
table.insert(maze, row)
end
file:close()
return maze
end
local function writeMaze(filename, maze)
local file = io.open(filename, "w")
if not file then
error("Could not open write file " .. filename)
end
for _, row in ipairs(maze) do
file:write(table.concat(row) .. "\n")
end
file:close()
end
local function findSymbol(maze, symbol)
local symbols = {}
for i = 1, #maze do
for j = 1, #maze[i] do
if maze[i][j] == symbol then
table.insert(symbols, {i,j})
end
end
end
if not symbols then
return nil
end
return symbols
end
local function bfs(maze, start_i, start_j, end_i, end_j)
local directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
local queue = {{start_i, start_j}}
local visited = {}
for i = 1, #maze do
visited[i] = {}
end
while #queue > 0 do
local current = table.remove(queue, 1)
local i, j = current[1], current[2]
if i == end_i and j == end_j then
local path = {}
visited[start_i][start_j] = nil
table.insert(path, {end_i, end_j})
while visited[i][j] do
local previous = visited[i][j]
table.insert(path, {previous[1], previous[2]})
i, j = previous[1], previous[2]
end
return path
end
for _, direction in ipairs(directions) do
local ni, nj = i + direction[1], j + direction[2]
if ni > 0 and nj > 0 and ni <= #maze and nj <= #maze[i]
and maze[ni] and maze[ni][nj] ~= '0' and not visited[ni][nj] then
visited[ni][nj] = {i, j}
table.insert(queue, {ni, nj})
end
end
end
return nil
end
local function markPath(maze, path)
for _, pos in ipairs(path) do
local i, j = pos[1], pos[2]
if maze[i][j] ~= 'E' and maze[i][j] ~= 'I' then
maze[i][j] = '*'
end
end
end
local function solveMaze(inputFile, outputFile)
local maze = readMaze(inputFile)
local start_i, start_j = {},{}
start_i, start_j = findSymbol(maze, 'I')[1], findSymbol(maze, 'I')[2]
local end_i, end_j = {},{}
end_i, end_j = findSymbol(maze, 'E')[1], findSymbol(maze, 'E')[2]
if not (start_i and start_j and end_i and end_j) then
print("Start or end symbol not found in maze")
return nil;
end
local path = {}
for i_start = 1, #start_i do
for j_start = 1, #start_j do
for i_end = 1, #start_i do
for j_end = 1, #start_j do
talbe.insert(path, bfs(maze, start_i[i_start], start_j[j_start], end_i[end_i], end_j[end_j]))
end
end
end
end
for i = 1, #path do
if path[i] then
markPath(maze, path)
writeMaze(outputFile, maze)
print("Path found and saved to " .. outputFile)
else
print("No path found from I to E")
end
end
end
solveMaze("Maze.txt", "Output.txt")