-
Notifications
You must be signed in to change notification settings - Fork 26
/
297.go
61 lines (53 loc) · 1.12 KB
/
297.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
// UVa 297 - Quadtrees
package main
import (
"fmt"
"os"
"strings"
)
const totalWeight = 1024
func preOrder(tree string, weight int, image *string) string {
if weight == totalWeight {
if tree[0] == 'p' {
return preOrder(tree[1:], weight/4, image)
}
if len(tree) == 1 {
*image += strings.Repeat(tree, weight)
return ""
}
}
for i := 0; i < 4; i++ {
if tree[0] == 'p' {
tree = preOrder(tree[1:], weight/4, image)
} else {
*image += strings.Repeat(string(tree[0]), weight)
tree = tree[1:]
}
}
return tree
}
func add(tree1, tree2 string) int {
var image1, image2 string
preOrder(tree1, totalWeight, &image1)
preOrder(tree2, totalWeight, &image2)
total := 0
for i := range image1 {
if image1[i] == 'f' || image2[i] == 'f' {
total++
}
}
return total
}
func main() {
in, _ := os.Open("297.in")
defer in.Close()
out, _ := os.Create("297.out")
defer out.Close()
var kase int
var tree1, tree2 string
for fmt.Fscanf(in, "%d", &kase); kase > 0; kase-- {
fmt.Fscanf(in, "%s", &tree1)
fmt.Fscanf(in, "%s", &tree2)
fmt.Fprintf(out, "There are %d black pixels.\n", add(tree1, tree2))
}
}