Compare commits
3 Commits
4f79d6f1ed
...
27cb446573
Author | SHA1 | Date | |
---|---|---|---|
|
27cb446573 | ||
|
7cb4fa99bc | ||
|
e53dac17d5 |
4
main.go
4
main.go
@ -131,12 +131,12 @@ func parse_arguments() (*reader.ReaderFactory, *writer.WriterFactory, *solver.So
|
|||||||
|
|
||||||
cellSizeIn := argparser.Int("", "cell-size-in", &argparse.Options{
|
cellSizeIn := argparser.Int("", "cell-size-in", &argparse.Options{
|
||||||
Help: "Size of a cell (in pixels) for input file of image type",
|
Help: "Size of a cell (in pixels) for input file of image type",
|
||||||
Default: 20,
|
Default: 3,
|
||||||
})
|
})
|
||||||
|
|
||||||
cellSizeOut := argparser.Int("", "cell-size-out", &argparse.Options{
|
cellSizeOut := argparser.Int("", "cell-size-out", &argparse.Options{
|
||||||
Help: "Size of a cell (in pixels) for output file of image type",
|
Help: "Size of a cell (in pixels) for output file of image type",
|
||||||
Default: 20,
|
Default: 3,
|
||||||
})
|
})
|
||||||
|
|
||||||
solverFactory.Type = argparser.Selector("a", "algo", solver.TYPES, &argparse.Options{
|
solverFactory.Type = argparser.Selector("a", "algo", solver.TYPES, &argparse.Options{
|
||||||
|
130
solver/bfs.go
130
solver/bfs.go
@ -1,9 +1,129 @@
|
|||||||
package solver
|
package solver
|
||||||
|
|
||||||
import "maze-solver/maze"
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"maze-solver/maze"
|
||||||
|
"maze-solver/utils"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type Bfs struct{}
|
type BFSSolver struct {
|
||||||
|
solver
|
||||||
func (*Bfs) Solve(maze *maze.Maze) *maze.SolvedMaze {
|
queue *Queue
|
||||||
return nil
|
}
|
||||||
|
|
||||||
|
type Queue struct {
|
||||||
|
head, tail *Element
|
||||||
|
}
|
||||||
|
|
||||||
|
type Element struct {
|
||||||
|
prev, next *Element
|
||||||
|
value []*maze.Node
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queue) enqueue(v []*maze.Node) {
|
||||||
|
prev_last := q.tail
|
||||||
|
new_elem := &Element{
|
||||||
|
prev: prev_last,
|
||||||
|
next: nil,
|
||||||
|
value: v,
|
||||||
|
}
|
||||||
|
if prev_last != nil {
|
||||||
|
prev_last.next = new_elem
|
||||||
|
}
|
||||||
|
|
||||||
|
q.tail = new_elem
|
||||||
|
|
||||||
|
if q.head == nil {
|
||||||
|
q.head = new_elem
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queue) dequeue() ([]*maze.Node, error) {
|
||||||
|
if q.head == nil {
|
||||||
|
return nil, errors.New("Can't dequeue and empty queue")
|
||||||
|
}
|
||||||
|
ret := q.head.value
|
||||||
|
q.head = q.head.next
|
||||||
|
if q.head != nil {
|
||||||
|
q.head.prev = nil
|
||||||
|
} else {
|
||||||
|
q.tail = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q Queue) String() string {
|
||||||
|
var ret strings.Builder
|
||||||
|
i := 0
|
||||||
|
for history := q.head; history != nil; history = history.next {
|
||||||
|
ret.WriteString(fmt.Sprintf("%v: %v\n", i, history_str(history.value)))
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
return ret.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func history_str(history []*maze.Node) string {
|
||||||
|
var ret strings.Builder
|
||||||
|
for _, node := range history {
|
||||||
|
ret.WriteString(fmt.Sprintf("%v ", node.Coords))
|
||||||
|
}
|
||||||
|
return ret.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,
|
||||||
|
tail: nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *BFSSolver) addIfNotVisited(node *maze.Node, current_history []*maze.Node) {
|
||||||
|
if !s.wasVisited(node) {
|
||||||
|
new_history := make([]*maze.Node, len(current_history)+1)
|
||||||
|
copy(new_history, current_history)
|
||||||
|
new_history[len(current_history)] = node
|
||||||
|
|
||||||
|
s.queue.enqueue(new_history)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,18 +6,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type DFSSolver struct {
|
type DFSSolver struct {
|
||||||
visited map[*maze.Node]bool
|
solver
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DFSSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
|
func (s *DFSSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
|
||||||
defer utils.Timer("Turn left 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]
|
||||||
s.visited = make(map[*maze.Node]bool, len(m.Nodes))
|
|
||||||
|
|
||||||
for _, node := range m.Nodes {
|
|
||||||
s.visited[node] = false
|
|
||||||
}
|
|
||||||
|
|
||||||
stack := make([]*maze.Node, 0, len(m.Nodes))
|
stack := make([]*maze.Node, 0, len(m.Nodes))
|
||||||
stack = append(stack, current)
|
stack = append(stack, current)
|
||||||
@ -53,11 +49,3 @@ func (s *DFSSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
|
|||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DFSSolver) wasVisited(node *maze.Node) bool {
|
|
||||||
if node == nil {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
visited, _ := s.visited[node]
|
|
||||||
return visited
|
|
||||||
}
|
|
||||||
|
@ -9,22 +9,46 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
_TURN_LEFT = "turn-left"
|
_DFS = "dfs"
|
||||||
|
_BFS = "bfs"
|
||||||
)
|
)
|
||||||
|
|
||||||
var TYPES = []string{
|
var TYPES = []string{
|
||||||
_TURN_LEFT,
|
_DFS,
|
||||||
|
_BFS,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *SolverFactory) Get() Solver {
|
func (f *SolverFactory) Get() Solver {
|
||||||
switch *f.Type {
|
switch *f.Type {
|
||||||
case _TURN_LEFT:
|
case _DFS:
|
||||||
return &DFSSolver{}
|
return &DFSSolver{}
|
||||||
|
case _BFS:
|
||||||
|
return &BFSSolver{}
|
||||||
}
|
}
|
||||||
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 {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user