from collections import deque # ì, í, ì¢, ì° íìì ìí ë°©í¥ ë²¡í° dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] def bfs(x, y, visited, grid): queue = deque([(x, y)]) # íì¬ ìì¹ë¥¼ 방문íë¤ê³ íì visited[x][y] = True while queue: x, y = queue.popleft() print(f"방문 ìì¹: ({x}, {y})") # íì¬ ìì¹ìì ì¸ì í ìì¹ íì for d in range(4): nx, ny = x + dx[d], y + dy[d] # ë°°ì´ ë²ì ìì ìê³ ë°©ë¬¸íì§ ììë¤ë©´ íì ë£ê³ íì if 0 <= nx < len(grid) and 0 <= ny < len(grid[0]) and not visited[nx][ny] and grid[nx][ny] == 1: queue.append((nx, ny)) visited[nx][ny] = True # 2ì°¨ì ë°°ì´ ìì (1ì ê° ì ìë ê²½ë¡, 0ì ê° ì ìë ê²½ë¡) grid = [ [1, 1, 1, 1], [1, 0, 1, 1], [0, 1, 1, 0], [0, 1, 1, 1], ] # 방문 ì ë³´ visited = [[False] * len(grid[0]) for _ in range(len(grid))] # ììë¡ (0, 0)ë¶í° íì ìì bfs(0, 0, visited, grid)