π λ¬Έμ μ΄ν΄
2μ°¨μ λ°°μ΄μ κ° μμλ κ·Έ μ§μμ λμ΄λ₯Ό λ΄κ³ μλ€.
λ¬Ό λμ΄κ° μ¦κ°ν¨μ λ°λΌ μΌλΆ μ§μμ μ κΈ°κ² λκ³ , μ κΈ°μ§ μμ μ°μλ μ§μμ μμ μμμ΄λΌ νλ€.
λ¬Ό λμ΄μ λ³νμ λ°λΌ μμ μμμ κ°μλ λ³νκ² λλλ°, μ΄ μμ μμ κ°μμ μ΅λκ°μ ꡬνλ λ¬Έμ μ΄λ€.
2μ°¨μ λ°°μ΄μ μ΅λ ν¬κΈ°λ 100*100μΌλ‘ μ μμ‘°μ¬λ₯Ό ν΄λ 10000λ² λ°μ λμ§ μλλ€.
1. νμ¬ μ 체 μμμ μ΅μ λμ΄~μ΅λ λμ΄ λ²μ λ΄μμ νμμ μμνλ€.
2. μ 체 μμμμ (0, 0) μμΉλΆν° μΆλ°ν΄μ νμ¬ λ¬Ό λμ΄ κΈ°μ€λ³΄λ€ λκ³ , λ°©λ¬Έν μ μλ κ³³μ λ§λλ©΄,
κ±°κΈ°μλΆν° λλΉ μ°μ νμμ μννλ©° 방문체ν¬λ₯Ό νλ€.
κ·Έλ¦¬κ³ νμ¬ λ¬Ό λμ΄μμμ μμ μμ κ°μλ₯Ό 1 μ¦κ°μν¨λ€.
3. λͺ¨λ μμμ νμνμΌλ©΄ νμ¬ λ¬Ό λμ΄ κΈ°μ€ μμ μμ κ°μμ μ§κΈκΉμ§μ μ΅λ μμ μμ κ°μλ₯Ό λΉκ΅ν΄μ μ΅λκ°μΌλ‘ κ°±μ νλ€.
ꡬν μΈμ΄: Python
from collections import deque
def bfs(t_x, t_y, height):
queue = deque()
# μ ν μ’ μ°
mx = [-1, 1, 0, 0]
my = [0, 0, -1, 1]
# μμμ
visited[t_x][t_y] = True
queue.append((t_x, t_y))
while queue:
x, y = queue.popleft()
for k in range(4):
nx = x + mx[k]
ny = y + my[k]
if 0 <= nx < n and 0 <= ny < n:
if area[nx][ny] > height and not visited[nx][ny]:
visited[nx][ny] = True
queue.append((nx, ny))
n = int(input())
area = [list(map(int, input().split())) for _ in range(n)]
min_height = min(map(min, area)) # μ΅μ λ¬Όλμ΄
max_height = max(map(max, area)) # μ΅λ λ¬Όλμ΄
answer = 1
for h in range(min_height, max_height + 1):
temp_cnt = 0
visited = [[False] * n for _ in range(n)] # 방문체ν¬
for i in range(n):
for j in range(n):
if area[i][j] > h and not visited[i][j]:
bfs(i, j, h)
temp_cnt += 1
answer = max(answer, temp_cnt)
print(answer)
'Problem_Solving' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] λ°±μ€ 9205 λ§₯μ£Ό λ§μλ©΄μ κ±Έμ΄κ°κΈ° / BFS (0) | 2021.02.08 |
---|---|
[BOJ] λ°±μ€ 2573 λΉμ° / BFS (0) | 2021.01.14 |
[BOJ] λ°±μ€ 5014 μ€ννΈλ§ν¬ / BFS (0) | 2021.01.13 |
[BOJ] λ°±μ€ 13549 μ¨λ°κΌμ§ 3 / BFS (0) | 2021.01.12 |
[BOJ] λ°±μ€ 13913 μ¨λ°κΌμ§ 4 / BFS (0) | 2021.01.12 |