-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path785.go
57 lines (49 loc) · 959 Bytes
/
785.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
// UVa 785 - Grid Colouring
package main
import (
"bufio"
"fmt"
"io"
"os"
)
var (
grid [][]byte
directions = [][2]int{{0, -1}, {1, 0}, {0, 1}, {-1, 0}}
)
func dfs(x, y int, cell byte) {
for _, direction := range directions {
if newY, newX := y+direction[1], x+direction[0]; grid[newY][newX] == ' ' {
grid[newY][newX] = cell
dfs(newX, newY, cell)
}
}
}
func solve(out io.Writer) {
for y, row := range grid {
for x, cell := range row {
if cell != 'X' && cell != ' ' {
dfs(x, y, cell)
}
}
}
for _, row := range grid {
fmt.Fprintln(out, string(row))
}
}
func main() {
in, _ := os.Open("785.in")
defer in.Close()
out, _ := os.Create("785.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
for s.Scan() {
if line := s.Text(); line == "_____________________________" {
solve(out)
fmt.Fprintln(out, line)
grid = nil
} else {
grid = append(grid, []byte(line))
}
}
}