refactor: removed map for visited and added field to node

This commit is contained in:
Karma Riuk 2023-08-15 15:41:24 +02:00
parent ee9988b972
commit 408b4c84c4
4 changed files with 8 additions and 38 deletions

View File

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

View File

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

View File

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

View File

@ -9,10 +9,6 @@ type Solver interface {
Solve(*maze.Maze) *maze.SolvedMaze Solve(*maze.Maze) *maze.SolvedMaze
} }
type solver struct {
visited map[*maze.Node]bool
}
type SolverFactory struct { type SolverFactory struct {
Type *string Type *string
} }
@ -37,18 +33,6 @@ func (f *SolverFactory) Get() Solver {
panic(fmt.Sprintf("Unrecognized solver type %q", *f.Type)) panic(fmt.Sprintf("Unrecognized solver type %q", *f.Type))
} }
func (s *solver) wasVisited(node *maze.Node) bool { func wasVisited(node *maze.Node) bool {
if node == nil { return node == nil || node.Visited
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
}
} }