You are viewing a single comment's thread. Return to all comments →
def countLuck(matrix, k): def valid_paths (i, j): return ([], [[i-1, j]])[valid_path(i-1, j)] \ + ([], [[i+1, j]])[valid_path(i+1, j)] \ + ([], [[i, j-1]])[valid_path(i, j-1)] \ + ([], [[i, j+1]])[valid_path(i, j+1)] m, n = len(matrix), len(matrix[0]) valid_path = lambda i, j: i >= 0 and i < m\ and j >= 0 and j < n\ and (matrix[i][j] == '.' or matrix[i][j] == '*') for i in range(m): matrix[i] = list(matrix[i]) for i in range(m): try: j = matrix[i].index('M') break except: continue check_path = [([i, j], 0)] while check_path: (i, j), i_waves = check_path.pop() possible_paths = valid_paths(i, j) if matrix[i][j] == '*': return ('Oops!','Impressed')[i_waves == k] matrix [i][j] = 'X' if len(possible_paths) == 1: check_path += [(possible_paths[0], i_waves)] else: for coord in possible_paths: check_path += [(coord, i_waves+1)]
Seems like cookies are disabled on this browser, please enable them to open this website
Count Luck
You are viewing a single comment's thread. Return to all comments →
Python 3