-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.rb
79 lines (70 loc) · 2.14 KB
/
maze.rb
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
require "set"
Point = Struct.new(:x, :y)
class Maze
CurrentPosition = Struct.new(:pos, :steps)
def initialize(maze, rows = 13, cols = 38)
@maze = maze
@rows = 13
@cols = 38
end
def bfs(source, target)
start_pos = @maze.index(source)
start_x = start_pos / @cols
start_y = start_pos % @cols - 1
if start_y < 0
start_y = @cols - 1
end
start_point = Point.new(start_x, start_y)
visited = [start_point].to_set
next_queue = [CurrentPosition.new(start_point, 0)]
while !next_queue.empty?
temp = next_queue.pop
if get_maze_position(temp.pos) == target
return true, temp.steps
end
#check if can move right
new_pos = Point.new(temp.pos.x + 1, temp.pos.y)
if is_valid_position(new_pos) && !visited.include?(new_pos)
visited.add(new_pos)
next_queue.push(CurrentPosition.new(new_pos, temp.steps + 1))
end
#check if can move left
new_pos = Point.new(temp.pos.x - 1, temp.pos.y)
if is_valid_position(new_pos) && !visited.include?(new_pos)
visited.add(new_pos)
next_queue.push(CurrentPosition.new(new_pos, temp.steps + 1))
end
#check if can move up
new_pos = Point.new(temp.pos.x, temp.pos.y + 1)
if is_valid_position(new_pos) && !visited.include?(new_pos)
visited.add(new_pos)
next_queue.push(CurrentPosition.new(new_pos, temp.steps + 1))
end
#check if can move down
new_pos = Point.new(temp.pos.x, temp.pos.y - 1)
if is_valid_position(new_pos) && !visited.include?(new_pos)
visited.add(new_pos)
next_queue.push(CurrentPosition.new(new_pos, temp.steps + 1))
end
end
return false, 0
end
def is_valid_position(point)
(point.x >= 0 && point.x < @rows) && (point.y >= 0 && point.y < @cols) && get_maze_position(point) != "#"
end
def get_maze_position(point)
index = (point.x * @cols) + point.y + 1
if index >= @maze.length
return "-1"
end
return @maze[index]
end
def solvable?
result, steps = bfs("A", "B")
return result
end
def steps
result, steps = bfs("A", "B")
return steps
end
end