-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10452.go
59 lines (52 loc) · 1.08 KB
/
10452.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
// UVa 10452 - Marcus
package main
import (
"fmt"
"os"
"strings"
)
var (
paths []string
m, n int
stones = "IEHOVA#"
directions = map[string][2]int{"forth": {0, -1}, "left": {-1, 0}, "right": {1, 0}}
grid [][]byte
)
func findStart() int {
for i, vi := range grid[m-1] {
if vi == '@' {
return i
}
}
return -1
}
func dfs(step, x, y int, path []string) {
if step == len(stones) {
paths = append(paths, strings.Join(path, " "))
return
}
for k, v := range directions {
if nx, ny := x+v[0], y+v[1]; nx >= 0 && nx < n && ny >= 0 && ny < m && grid[ny][nx] == stones[step] {
dfs(step+1, nx, ny, append(path, k))
}
}
}
func main() {
in, _ := os.Open("10452.in")
defer in.Close()
out, _ := os.Create("10452.out")
defer out.Close()
var line string
var kase int
for fmt.Fscanf(in, "%d", &kase); kase > 0; kase-- {
fmt.Fscanf(in, "%d%d", &m, &n)
grid = make([][]byte, m)
for i := range grid {
fmt.Fscanf(in, "%s", &line)
grid[i] = []byte(line)
}
paths = nil
dfs(0, findStart(), m-1, nil)
fmt.Fprintln(out, strings.Join(paths, "\n"))
}
}