-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path657.go
105 lines (95 loc) · 2.03 KB
/
657.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// UVa 657 - The die is cast
package main
import (
"fmt"
"os"
"sort"
)
var (
w, h, count int
picture [][]byte
directions = [][2]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}}
visited [][]bool
dots []int
)
func dfs2(y, x int) {
for _, direction := range directions {
if newY, newX := y+direction[1], x+direction[0]; newY >= 0 && newY < h && newX >= 0 && newX < w && picture[newY][newX] == 'X' && !visited[newY][newX] {
visited[newY][newX] = true
dfs2(newY, newX)
}
}
}
func dfs1(y, x int) {
for _, direction := range directions {
if newY, newX := y+direction[1], x+direction[0]; newY >= 0 && newY < h && newX >= 0 && newX < w && !visited[newY][newX] {
visited[newY][newX] = true
switch picture[newY][newX] {
case '*':
dfs1(newY, newX)
case 'X':
dfs2(newY, newX)
count++
}
}
}
}
func dfs(y, x int) {
for _, direction := range directions {
if newY, newX := y+direction[1], x+direction[0]; newY >= 0 && newY < h && newX >= 0 && newX < w && !visited[newY][newX] {
visited[newY][newX] = true
switch picture[newY][newX] {
case '.':
dfs(newY, newX)
case '*':
count = 0
dfs1(newY, newX)
if count > 0 {
dots = append(dots, count)
}
}
}
}
}
func startPoint() (int, int) {
for y, row := range picture {
for x := range row {
if picture[y][x] == '.' {
return y, x
}
}
}
return -1, -1
}
func solve() []int {
visited = make([][]bool, h)
for i := range visited {
visited[i] = make([]bool, w)
}
y, x := startPoint()
visited[y][x] = true
dots = nil
dfs(y, x)
sort.Ints(dots)
return dots
}
func main() {
in, _ := os.Open("657.in")
defer in.Close()
out, _ := os.Create("657.out")
defer out.Close()
var line string
for kase := 1; ; kase++ {
if fmt.Fscanf(in, "%d%d", &w, &h); w == 0 && h == 0 {
break
}
picture = make([][]byte, h)
for i := range picture {
fmt.Fscanf(in, "%s", &line)
picture[i] = []byte(line)
}
fmt.Fprintf(out, "Throw %d\n", kase)
str := fmt.Sprintln(solve())
fmt.Fprintf(out, "%s\n\n", str[1:len(str)-2])
}
}