문제 링크 :https://programmers.co.kr/learn/courses/30/lessons/81302
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | dy = [0, 0, –1, 1] dx = [–1, 1, 0, 0] flag = 1 def solution(places): global flag answer = [] for i in places: flag = 1 for j in range(5): for k in range(5): if i[j][k] == ‘P’: dfs(i, (j, k), (–1, –1), 2) answer.append(flag) return answer def dfs(arr, cur, before, n): global flag a = [] y, x = cur by, bx = before for i in range(4): ny = y + dy[i] nx = x + dx[i] if 0 <= nx < 5 and 0 <= ny < 5 and arr[ny][nx] != ‘X’: if ny == by and nx == bx: continue if arr[ny][nx] == ‘P’: flag = 0 return if arr[ny][nx] == ‘O’: a.append((ny, nx)) for i in a: ny, nx = i if n > 1: dfs(arr, (ny, nx), (y, x), n – 1) | cs |