You’re given a board game which is a row of squares, each labeled with an integer. This can be represented by a list, e.g. [1, 3, 2, 0, 5, 2, 8, 4, 1] Given a start position on the board, you “win” by landing on a zero, where you move by jumping from square to square either left or right the number of spaces specified on the square you’re currently on. Your task is to implement the function: def can_win(board, pos): returns True if you can win the board from that starting pos, False otherwise
Solution:
用一个visited array记录访问过的格子。
先check pos是不是out of bound
然后 check pos 是不是visited过。
然后 check array[pos]是不是等于0
然后 如果visited[pos] == 0 {
visted[pos] = 1
res = dfs(array, pos - array[pos]) || dfs(array, pos + array[pos])
visited[pos] = 0
}
return res;