No.196 - LeetCode841. Keys and Rooms - 经典bfs遍历

func canVisitAllRooms(rooms [][]int) bool {
	n := len(rooms)
    if n == 1 {
        return true
    }
	color := make([]int,n)
    color[0] = 1
	mp := make([][]int,n)
	for i,a := range rooms {
        mp[i] = append(mp[i], a...)
	}
	dfs(0, mp, color)
	for _,t := range color {
		if t == 0 {
			return false
		}
	}
	return true
}

func dfs(now int, mp [][]int, color []int){
	for _,t := range mp[now] {
		if color[t] == 0 {
			color[t] = 1
			dfs(t,mp,color)
		}
	}
}

你可能感兴趣的:(leetcode)