-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path416.go
69 lines (61 loc) · 1.07 KB
/
416.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
// UVa 416 - LED Test
package main
import (
"fmt"
"os"
)
var (
n int
segments []int
digitMap = map[int]int{0: 126, 1: 48, 2: 109, 3: 121, 4: 51, 5: 91, 6: 95, 7: 112, 8: 127, 9: 123}
)
func toBinary(line string) int {
var total int
for i, ch := range line {
if ch == 'Y' {
total += 1 << uint(6-i)
}
}
return total
}
func dfs(curr, level, mask int) bool {
if level == n {
return true
}
for i := 0; i <= mask; i++ {
if i&mask == i && digitMap[curr]&i == segments[level] && dfs(curr-1, level+1, i) {
return true
}
}
return false
}
func solve() bool {
for i := 9; i >= n-1; i-- {
if dfs(i, 0, 127) {
return true
}
}
return false
}
func main() {
in, _ := os.Open("416.in")
defer in.Close()
out, _ := os.Create("416.out")
defer out.Close()
var line string
for {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
segments = make([]int, n)
for i := range segments {
fmt.Fscanf(in, "%s", &line)
segments[i] = toBinary(line)
}
if solve() {
fmt.Fprintln(out, "MATCH")
} else {
fmt.Fprintln(out, "MISMATCH")
}
}
}