[자료구조] FloodFill 파이썬 (BFS, queue)
BFS from collections import deque def solution(n, m, image): cnt = 0 visited = [[False] * m for _ in range(n)] # visited table (col(m) x row(n)) dir = [[1,0], [0,1], [-1,0], [0,-1]] # 4-direction for i in range(n): for j in range(m): if not visited[i][j]: q = deque() # BFS-queue q.append([i,j]) visited[i][j] = True color = image[i][j] while q: x, y = q.popleft() for dx, dy in dir: xx, yy = x+dx,..
2021. 4. 23.
[프로그래머스] 단어 변환 파이썬 (BFS/DFS)
고급 from collections import deque as queue transistable = lambda a,b: sum((1 if x!=y else 0) for x,y in zip(a,b)) == 1 def solution(begin,target,words): q, d = queue(), dict() q.append((begin, 0)) d[begin] = set(filter(lambda x:transistable(x,begin), words)) for w in words: d[w] = set(filter(lambda x:transistable(x,w), words)) while q: cur, level = q.popleft() if level > len(words): return 0 for ..
2021. 3. 17.