-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10422.go
66 lines (59 loc) · 1.32 KB
/
10422.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// UVa 10422 - Knights in FEN
package main
import (
"fmt"
"os"
)
const max = 11
var (
steps int
board [5][5]byte
final = [5][5]byte{
{'1', '1', '1', '1', '1'},
{'0', '1', '1', '1', '1'},
{'0', '0', ' ', '1', '1'},
{'0', '0', '0', '0', '1'},
{'0', '0', '0', '0', '0'},
}
directions = [][2]int{{-2, -1}, {-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}}
)
func dfs(y, x, level int) {
if level >= steps {
return
}
if final == board {
steps = level
return
}
for _, direction := range directions {
if newY, newX := y+direction[0], x+direction[1]; newY >= 0 && newY < 5 && newX >= 0 && newX < 5 {
board[y][x], board[newY][newX] = board[newY][newX], board[y][x]
dfs(newY, newX, level+1)
board[y][x], board[newY][newX] = board[newY][newX], board[y][x]
}
}
}
func main() {
in, _ := os.Open("10422.in")
defer in.Close()
out, _ := os.Create("10422.out")
defer out.Close()
var y, x, kase int
for fmt.Fscanf(in, "%d", &kase); kase > 0; kase-- {
for i, row := range board {
for j := range row {
if fmt.Fscanf(in, "%c", &board[i][j]); board[i][j] == ' ' {
y, x = i, j
}
}
fmt.Fscanln(in)
}
steps = max
dfs(y, x, 0)
if steps == max {
fmt.Fprintln(out, "Unsolvable in less than 11 move(s).")
} else {
fmt.Fprintf(out, "Solvable in %d move(s).\n", steps)
}
}
}