Skip to content

在你这里学习了,谢谢。并附上 0351 Android Unlock Pattern 的golang题解 #2

@ghost

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions