Skip to content

Instantly share code, notes, and snippets.

@fangdingjun
Created October 14, 2015 03:09
Show Gist options
  • Save fangdingjun/692a02531e7e3ef3b842 to your computer and use it in GitHub Desktop.
Save fangdingjun/692a02531e7e3ef3b842 to your computer and use it in GitHub Desktop.
This code implement the Mouse In a Maze problem
package main
import (
"fmt"
)
var maze [][]int = [][]int{
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
{2, 0, 0, 0, 2, 0, 0, 0, 2, 2},
{2, 0, 2, 0, 2, 0, 2, 2, 0, 2},
{2, 0, 0, 0, 0, 0, 0, 2, 0, 2},
{2, 2, 0, 2, 0, 2, 0, 0, 0, 2},
{2, 0, 2, 0, 0, 0, 2, 0, 2, 2},
{2, 0, 0, 0, 2, 0, 2, 0, 0, 2},
{2, 0, 2, 2, 0, 0, 0, 0, 2, 2},
{2, 0, 0, 0, 0, 2, 2, 0, 0, 2},
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
}
type point struct {
x int
y int
}
func (p *point) equal(p1 *point) bool {
return p.x == p1.x && p.y == p1.y
}
func pp(x, y int) *point {
return &point{x, y}
}
var start, end *point
func main() {
start = pp(1, 1)
end = pp(8, 8)
show()
visit(start)
//show()
}
func visit(p *point) {
//fmt.Printf("x=%d, y=%d\n", p.x, p.y)
if maze[p.x][p.y] != 0 {
return
}
maze[p.x][p.y] = 1
if p.equal(end) {
show()
//return
//maze[p.x][p.y]=0
}
visit(pp(p.x, p.y+1))
visit(pp(p.x+1, p.y))
visit(pp(p.x, p.y-1))
visit(pp(p.x-1, p.y))
maze[p.x][p.y] = 0
}
func show() {
fmt.Printf("\n")
for i := 0; i < 10; i++ {
for j := 0; j < 10; j++ {
switch maze[i][j] {
case 2:
fmt.Printf(" =")
case 1:
fmt.Printf(" *")
case 0:
fmt.Printf(" .")
}
}
fmt.Printf("\n")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment