3 Commits

5 changed files with 99 additions and 40 deletions

View File

@ -30,6 +30,7 @@ type Node struct {
Coords Coordinates
Up, Down *Node
Left, Right *Node
Visited bool `default:"false"`
}
func NewNode(coords Coordinates) *Node {

View File

@ -9,7 +9,6 @@ import (
)
type BFSSolver struct {
solver
queue *Queue
}
@ -76,8 +75,6 @@ func history_str(history []*maze.Node) string {
func (s *BFSSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
defer utils.Timer("BFS algorithm", 2)()
s.initVisited(m)
current, end := m.Nodes[0], m.Nodes[len(m.Nodes)-1]
s.queue = &Queue{
head: nil,
@ -87,20 +84,15 @@ func (s *BFSSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
current_history := make([]*maze.Node, 0, len(m.Nodes))
current_history = append(current_history, current)
// fmt.Printf("end.Coords: %v\n", end.Coords)
var err error
for current != end {
// fmt.Printf("current.Coords: %v\n", current.Coords)
s.visited[current] = true
current.Visited = true
s.addIfNotVisited(current.Down, current_history)
s.addIfNotVisited(current.Left, current_history)
s.addIfNotVisited(current.Right, current_history)
s.addIfNotVisited(current.Up, current_history)
// fmt.Println(s.queue)
current_history, err = s.queue.dequeue()
if err != nil {
panic(err)
@ -108,10 +100,6 @@ func (s *BFSSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
current = current_history[len(current_history)-1]
}
// for i, node := range current_history {
// fmt.Printf("%v: %v\n", i, node.Coords)
// }
return &maze.SolvedMaze{
Maze: m,
Solution: current_history,
@ -119,7 +107,7 @@ func (s *BFSSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
}
func (s *BFSSolver) addIfNotVisited(node *maze.Node, current_history []*maze.Node) {
if !s.wasVisited(node) {
if !visited(node) {
new_history := make([]*maze.Node, len(current_history)+1)
copy(new_history, current_history)
new_history[len(current_history)] = node

View File

@ -5,23 +5,20 @@ import (
"maze-solver/utils"
)
type DFSSolver struct {
solver
}
type DFSSolver struct{}
func (s *DFSSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
defer utils.Timer("DFS algorithm", 2)()
s.initVisited(m)
current, end := m.Nodes[0], m.Nodes[len(m.Nodes)-1]
stack := make([]*maze.Node, 0, len(m.Nodes))
stack = append(stack, current)
for current != end {
s.visited[current] = true
current.Visited = true
left_visited, right_visited, up_visited, down_visited := s.wasVisited(current.Left), s.wasVisited(current.Right), s.wasVisited(current.Up), s.wasVisited(current.Down)
left_visited, right_visited, up_visited, down_visited := visited(current.Left), visited(current.Right), visited(current.Up), visited(current.Down)
if left_visited && right_visited && up_visited && down_visited {
// dead end or no more visited nodes

85
solver/dijkstra.go Normal file
View File

@ -0,0 +1,85 @@
package solver
import (
"maze-solver/maze"
"maze-solver/utils"
"slices"
"sort"
)
type DijkstraSolver struct {
dist_from_start map[*maze.Node]int
parent map[*maze.Node]*maze.Node
stack sorted_stack
}
type sorted_stack []*maze.Node
func (s *DijkstraSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
defer utils.Timer("Dijkstra algorithm", 2)()
s.dist_from_start = make(map[*maze.Node]int, len(m.Nodes))
s.parent = make(map[*maze.Node]*maze.Node, len(m.Nodes))
for _, node := range m.Nodes {
s.dist_from_start[node] = 0
}
current, end := m.Nodes[0], m.Nodes[len(m.Nodes)-1]
for current != end {
current.Visited = true
for _, child := range []*maze.Node{current.Left, current.Right, current.Up, current.Down} {
if child != nil {
dist := s.dist_from_start[current] + int(current.Coords.Distance(child.Coords))
if !child.Visited {
s.parent[child] = current
s.dist_from_start[child] = dist
s.stack.insert(child, &s.dist_from_start)
} else if s.dist_from_start[child] > dist {
s.parent[child] = current
s.dist_from_start[child] = dist
sort.Slice(s.stack, func(i, j int) bool {
return s.dist_from_start[s.stack[i]] < s.dist_from_start[s.stack[j]]
})
}
}
}
current = s.stack.pop()
}
solution := make([]*maze.Node, 0, len(m.Nodes))
for current != m.Nodes[0] {
solution = append(solution, current)
current = s.parent[current]
}
solution = append(solution, m.Nodes[0])
for i, j := 0, len(solution)-1; i < j; i, j = i+1, j-1 {
solution[i], solution[j] = solution[j], solution[i]
}
return &maze.SolvedMaze{
Maze: m,
Solution: solution,
}
}
func (s *sorted_stack) insert(node *maze.Node, dists *map[*maze.Node]int) {
var dummy *maze.Node
*s = append(*s, dummy) // extend the slice
i, _ := slices.BinarySearchFunc(*s, node, func(e, t *maze.Node) int {
return (*dists)[t] - (*dists)[e]
})
copy((*s)[i+1:], (*s)[i:]) // make room
(*s)[i] = node
}
func (s *sorted_stack) pop() *maze.Node {
last_i := len(*s) - 1
ret := (*s)[last_i]
*s = (*s)[:last_i]
return ret
}

View File

@ -9,10 +9,6 @@ type Solver interface {
Solve(*maze.Maze) *maze.SolvedMaze
}
type solver struct {
visited map[*maze.Node]bool
}
type SolverFactory struct {
Type *string
}
@ -20,11 +16,13 @@ type SolverFactory struct {
const (
_DFS = "dfs"
_BFS = "bfs"
_Dijkstra = "dijkstra"
)
var TYPES = []string{
_DFS,
_BFS,
_Dijkstra,
}
func (f *SolverFactory) Get() Solver {
@ -33,22 +31,12 @@ func (f *SolverFactory) Get() Solver {
return &DFSSolver{}
case _BFS:
return &BFSSolver{}
case _Dijkstra:
return &DijkstraSolver{}
}
panic(fmt.Sprintf("Unrecognized solver type %q", *f.Type))
}
func (s *solver) wasVisited(node *maze.Node) bool {
if node == nil {
return true
}
visited, _ := s.visited[node]
return visited
}
func (s *solver) initVisited(m *maze.Maze) {
s.visited = make(map[*maze.Node]bool, len(m.Nodes))
for _, node := range m.Nodes {
s.visited[node] = false
}
func visited(node *maze.Node) bool {
return node == nil || node.Visited
}