๐ค ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์ ๋ถ ํ์ํด์ผ ํ๋ ๊ฒฝ์ฐ๋ฅผ ์ํด
ex. ์ํ๊ณ
๊ณต๊ฐ
์ ์๊ตฌdef bfs(x, y):
directions = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, -1), (-1, 1)]
que = deque()
que.append((x, y))
island[x][y] = 0
while que:
n = que.popleft()
for i in range(8):
nx = n[0] + directions[i][0]
ny = n[1] + directions[i][1]
if (0<=nx<h) and (0<=ny<w):
if int(island[nx][ny]) == 1:
island[nx][ny] = 0
que.append((nx, ny))
def dfs(x, y):
# ํ์ฌ ์์(์ขํ) ๋ฐฉ๋ฌธ
visited[x][y] = True
cur_color = matrix[x][y]
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if (0<=nx<N) and (0<=ny<N):
# ํ์ฌ ์ขํ์ ์์๊ณผ ์ํ์ข์ฐ ์ขํ์ ์๋ ์์์ด ๊ฐ์ผ๋ฉด dfs๋ก ์ถ๊ฐ
if visited[nx][ny] == False:
if matrix[nx][ny] == cur_color:
dfs(nx, ny)