/
main.go
79 lines (63 loc) · 1.59 KB
/
main.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
package main
import (
"fmt"
"math"
"strings"
)
func countLetters(word string) int {
return len(strings.ReplaceAll(word, " ", ""))
}
func canFormWord(graph [][]int, letters []string, word string, visited map[int]bool) bool {
if word == "" {
return true
}
for i, letter := range letters {
fmt.Println("letter:", letter, "word:", word)
if letter == string(word[0]) && !visited[i] {
visited[i] = true
for range graph[i] {
if canFormWord(graph, letters, word[1:], visited) {
return true
}
}
visited[i] = false
}
}
return false
}
func scoreGame(graph [][]int, letters []string, words []string, isAlice bool) int {
score := math.MinInt32
for _, word := range words {
visited := make(map[int]bool)
fmt.Println(word)
if canFormWord(graph, letters, word, visited) {
wordLength := countLetters(word)
if (isAlice && wordLength%2 == 1) || (!isAlice && wordLength%2 == 0) {
score++
}
}
}
return score
}
func solve(A [][]int, B []string, C []string, D int) int {
graph := make([][]int, D)
for _, edge := range A {
graph[edge[0]] = append(graph[edge[0]], edge[1])
graph[edge[1]] = append(graph[edge[1]], edge[0])
}
aliceScore := scoreGame(graph, B, C, true)
bobScore := scoreGame(graph, B, C, false)
if aliceScore > bobScore || (aliceScore == bobScore && bobScore > 0) {
return 1
}
return 0
}
func main() {
input1 := [][]int{{1, 0}, {2, 1}, {0, 2}}
letters1 := []string{"b", "c", "a"}
words1 := []string{"cc", "a", "bc"}
D1 := 3
output1 := solve(input1, letters1, words1, D1)
fmt.Println("Input 1:", input1)
fmt.Println("Output 1:", output1)
}