-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem056.go
36 lines (32 loc) · 889 Bytes
/
problem056.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
package problem056
const NO_COLOR = -1
func isValid(nodeId int, color int, nodeColors []int, adjacency [][]bool) bool {
for otherNodeId, isAdjacent := range adjacency[nodeId] {
if isAdjacent && color == nodeColors[otherNodeId] {
return false
}
}
return true
}
func resolve(nodeId int, nodeColors []int, adjacency [][]bool, maxColor int) bool {
if nodeId >= len(nodeColors) {
return true
}
for color := 0; color < maxColor; color++ {
if isValid(nodeId, color, nodeColors, adjacency) {
nodeColors[nodeId] = color
if resolve(nodeId+1, nodeColors, adjacency, maxColor) {
return true
}
nodeColors[nodeId] = NO_COLOR
}
}
return false
}
func HasSolution(adjacency [][]bool, maxColor int) bool {
nodeColors := make([]int, maxColor, maxColor)
for i := range nodeColors {
nodeColors[i] = NO_COLOR
}
return resolve(0, nodeColors, adjacency, maxColor)
}