Skip to content

Instantly share code, notes, and snippets.

@momchil-velikov
Created November 21, 2016 10:39
Show Gist options
  • Save momchil-velikov/cc671725b38b3656f5e8a019820936cf to your computer and use it in GitHub Desktop.
Save momchil-velikov/cc671725b38b3656f5e8a019820936cf to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
const (
a = 8
b = 8
maxLevels = 64
)
func main() {
var chess [a][b]int
var current int
tryToMove(&chess, current, 3, 4)
printChess(chess)
}
func tryToMove(chess *[a][b]int, current int, x int, y int) bool {
if x >= a || y >= b || x < 0 || y < 0 {
return false
}
if chess[x][y] > 0 {
return false
}
current++
chess[x][y] = current
if current == maxLevels {
return true
}
if tryToMove(chess, current, x+2, y+1) {
return true
}
if tryToMove(chess, current, x+2, y-1) {
return true
}
if tryToMove(chess, current, x-2, y+1) {
return true
}
if tryToMove(chess, current, x-2, y-1) {
return true
}
if tryToMove(chess, current, x+1, y+2) {
return true
}
if tryToMove(chess, current, x+1, y-2) {
return true
}
if tryToMove(chess, current, x-1, y+2) {
return true
}
if tryToMove(chess, current, x-1, y-2) {
return true
}
chess[x][y] = 0
current--
return false
}
func printChess(chess [a][b]int) {
for i := 0; i < a; i++ {
for j := 0; j < b; j++ {
fmt.Printf("%3d", chess[i][j])
}
fmt.Println()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment