-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10377.go
78 lines (71 loc) · 1.53 KB
/
10377.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
67
68
69
70
71
72
73
74
75
76
77
78
// UVa 10377 - Maze Traversal
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
var (
row, column int
directions = [][2]int{{0, -1}, {1, 0}, {0, 1}, {-1, 0}}
directionMap = map[int]byte{0: 'N', 1: 'E', 2: 'S', 3: 'W'}
)
func solve(r, c int, maze [][]byte, command string) (int, int, byte) {
steps := []byte(strings.Replace(command, " ", "", -1))
var idx int
here:
for _, step := range steps {
switch step {
case 'R':
if idx++; idx == 4 {
idx = 0
}
case 'L':
if idx--; idx == -1 {
idx = 3
}
case 'F':
direction := directions[idx]
if newR, newC := r+direction[1], c+direction[0]; newR >= 0 && newR < row && newC >= 0 && newC < column && maze[newR][newC] == ' ' {
r, c = newR, newC
}
case 'Q':
break here
}
}
return r + 1, c + 1, directionMap[idx]
}
func main() {
in, _ := os.Open("10377.in")
defer in.Close()
out, _ := os.Create("10377.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var kase, r, c int
var maze [][]byte
s.Scan()
for fmt.Sscanf(s.Text(), "%d", &kase); kase > 0 && s.Scan(); kase-- {
s.Scan()
fmt.Sscanf(s.Text(), "%d%d", &row, &column)
maze = make([][]byte, row)
for i := range maze {
s.Scan()
maze[i] = []byte(s.Text())
}
s.Scan()
fmt.Sscanf(s.Text(), "%d%d", &r, &c)
var command string
for s.Scan() {
if command += s.Text(); strings.HasSuffix(command, "Q") {
break
}
}
y, x, d := solve(r-1, c-1, maze, command)
fmt.Fprintf(out, "%d %d %c\n", y, x, d)
if kase > 1 {
fmt.Fprintln(out)
}
}
}