-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Open

Description
var blocker = [][]int{
// 1 2 3 4 5 6 7 8 9 // first column is added so we use 1-9
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 0 phantom node. as the STARTING point
{0, 0, 0, 2, 0, 0, 0, 4, 0, 5}, // 1
{0, 0, 0, 0, 0, 0, 0, 0, 5, 0}, // 2
{0, 2, 0, 0, 0, 0, 0, 5, 0, 6}, // 3
{0, 0, 0, 0, 0, 0, 5, 0, 0, 0}, // 4
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 5
{0, 0, 0, 0, 5, 0, 0, 0, 0, 0}, // 6
{0, 4, 0, 5, 0, 0, 0, 0, 0, 8}, // 7
{0, 0, 5, 0, 0, 0, 0, 0, 0, 0}, // 8
{0, 5, 0, 6, 0, 0, 0, 8, 0, 0}, // 9
}
var M, N int
func numberOfPatterns(m, n int) int {
M = m
N = n
var work = make([]bool, 10, 10)
return count(work, 0, 0)
}
func count(work []bool, now int, already int) (res int) {
if already >= M && already <= N {
res++
} else if already > N {
return
}
for i, tmp := 1, 0; i != 10; i++ {
if work[i] { // if already visited
continue
}
tmp = blocker[now][i]
if tmp == 0 || work[tmp] {
work[i] = true
already++
res += count(work, i, already)
already--
work[i] = false
}
}
return
}
Metadata
Metadata
Assignees
Labels
No labels