-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path445.go
79 lines (70 loc) · 1.13 KB
/
445.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
79
// UVa 445 - Marvelous Mazes
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
var out io.WriteCloser
func nextLine(s *bufio.Scanner) (string, bool) {
if s.Scan() {
return s.Text(), true
}
return "", false
}
func outputLine(line string) {
cnt := 0
for i := range line {
v := line[i]
if v == 'b' {
v = ' '
}
if v >= '0' && v <= '9' {
cnt += int(v - '0')
} else {
for ; cnt > 0; cnt-- {
fmt.Fprintf(out, "%c", v)
}
}
}
fmt.Fprintln(out)
}
func solve(maze string) {
if maze[len(maze)-1] == '!' {
maze = maze[:len(maze)-1]
}
for _, v := range strings.Split(maze, "!") {
outputLine(v)
}
}
func main() {
in, _ := os.Open("445.in")
defer in.Close()
out, _ = os.Create("445.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var line, maze string
var ok bool
for first := true; ; {
if line, ok = nextLine(s); !ok || line == "" {
if first {
first = false
} else {
fmt.Fprintln(out)
}
solve(maze)
if !ok {
break
}
maze = ""
} else {
if maze != "" && maze[len(maze)-1] != '!' {
maze += "!"
}
maze += line
}
}
}