Moving right along
This commit is contained in:
@@ -104,11 +104,44 @@ func play(prog []int) {
|
||||
}
|
||||
}
|
||||
}()
|
||||
ret := p.Run()
|
||||
if ret == intcode.RET_DONE {
|
||||
maze.Print()
|
||||
} else if ret == intcode.RET_ERR {
|
||||
fmt.Println("ERROR")
|
||||
fmt.Println(p.Error())
|
||||
p.Run()
|
||||
//We force quit the program when the maze is built.
|
||||
// Now we find the quickest path through the maze.
|
||||
fmt.Println(helpers.CLEAR_SCREEN)
|
||||
maze.Print()
|
||||
fmt.Println("Now find shortest path")
|
||||
done, ret := BFS(maze.bfs[maze.startCoord], maze.bfs[maze.o2Coord])
|
||||
if done {
|
||||
fmt.Println("Found Route")
|
||||
} else {
|
||||
fmt.Println("No Route Found")
|
||||
}
|
||||
fmt.Println("Steps:", len(ret))
|
||||
}
|
||||
|
||||
func BFS(start, goal *BFSNode) (bool, []*BFSNode) {
|
||||
var queue, explored []*BFSNode
|
||||
queue = append(queue, start)
|
||||
if start == goal {
|
||||
return true, queue
|
||||
}
|
||||
explored = append(explored, start)
|
||||
for len(queue) > 0 {
|
||||
var current *BFSNode
|
||||
if len(queue) > 1 {
|
||||
current, queue = queue[0], queue[1:]
|
||||
if current == goal {
|
||||
return true, explored
|
||||
} else {
|
||||
children := current.getChildren()
|
||||
if len(children) != 0 {
|
||||
queue = append(queue, children...)
|
||||
} else {
|
||||
return false, explored
|
||||
}
|
||||
}
|
||||
}
|
||||
explored = append(explored, current)
|
||||
}
|
||||
return false, explored
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user