-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path10267.go
More file actions
101 lines (92 loc) · 2.03 KB
/
10267.go
File metadata and controls
101 lines (92 loc) · 2.03 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// UVa 10267 - Graphical Editor
package main
import (
"bufio"
"fmt"
"io"
"os"
)
var (
out io.WriteCloser
directions = [][2]int{{0, -1}, {1, 0}, {0, 1}, {-1, 0}}
picture [][]byte
m, n int
)
func clear(picture [][]byte) {
for i := range picture {
for j := range picture[i] {
picture[i][j] = 'O'
}
}
}
func fill(picture [][]byte, x, y int, c byte) {
old := picture[y][x]
picture[y][x] = c
for _, direction := range directions {
xn, yn := x+direction[1], y+direction[0]
if xn >= 0 && xn < m && yn >= 0 && yn < n && picture[yn][xn] == old {
fill(picture, xn, yn, c)
}
}
}
func solve(line string) {
var x1, y1, x2, y2 int
var tmp, c string
switch line[0] {
case 'I':
fmt.Sscanf(line, "%s%d%d", &tmp, &m, &n)
picture = make([][]byte, n)
for i := range picture {
picture[i] = make([]byte, m)
}
fallthrough
case 'C':
clear(picture)
case 'L':
fmt.Sscanf(line, "%s%d%d%s", &tmp, &x1, &y1, &c)
picture[y1-1][x1-1] = c[0]
case 'V':
fmt.Sscanf(line, "%s%d%d%d%s", &tmp, &x1, &y1, &y2, &c)
for y := min(y1, y2) - 1; y <= max(y1, y2)-1; y++ {
picture[y][x1-1] = c[0]
}
case 'H':
fmt.Sscanf(line, "%s%d%d%d%s", &tmp, &x1, &x2, &y1, &c)
for x := min(x1, x2) - 1; x <= max(x1, x2)-1; x++ {
picture[y1-1][x] = c[0]
}
case 'K':
fmt.Sscanf(line, "%s%d%d%d%d%s", &tmp, &x1, &y1, &x2, &y2, &c)
for x := x1 - 1; x <= x2-1; x++ {
for y := y1 - 1; y <= y2-1; y++ {
picture[y][x] = c[0]
}
}
case 'F':
fmt.Sscanf(line, "%s%d%d%s", &tmp, &x1, &y1, &c)
if picture[y1-1][x1-1] != c[0] { // so don't need to record visited when doing dfs
fill(picture, x1-1, y1-1, c[0])
}
case 'S':
fmt.Sscanf(line, "%s%s", &tmp, &c)
fmt.Fprintln(out, c)
for _, v := range picture {
fmt.Fprintln(out, string(v))
}
}
}
func main() {
in, _ := os.Open("10267.in")
defer in.Close()
out, _ = os.Create("10267.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var line string
for s.Scan() {
if line = s.Text(); line == "X" {
break
}
solve(line)
}
}