문제 : https://www.acmicpc.net/problem/2583
BFS 문제다.
입력에 맞게 벽을 맵에 넣는것, 출력이 살짝 다른것 빼고
https://www.acmicpc.net/problem/2667
이문제랑 똑같다.
import sys
from collections import deque
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
n, m, k = [int(i) for i in sys.stdin.readline().split()]
d = [[1]*m for _ in range(n)]
for _ in range(k):
ax, ay, bx, by = [int(i) for i in sys.stdin.readline().split()]
for i in range(ax, bx):
for j in range(ay, by):
d[j][i] = 0
v = [[False]*m for _ in range(n)]
ret = 0
val = []
for i in range(n):
for j in range(m):
q = deque()
block = deque()
if v[i][j] == True or d[i][j] == 0:
continue
v[i][j] = ret
q.append((i, j))
block.append((i, j))
while q:
y, x = q.popleft()
v[y][x] = True
for a in range(4):
cy, cx = y+dy[a], x+dx[a]
if 0 <= cy < n and 0 <= cx < m:
if v[cy][cx] == False and d[cy][cx] == 1:
q.append((cy, cx))
block.append((cy, cx))
v[cy][cx] = True
ret = ret + 1
val.append(len(block))
print(ret)
print(" ".join(str(i) for i in sorted(val)).strip())
'취준 > 백준' 카테고리의 다른 글
[백준][Python][1920][이분탐색] 수 찾기 (0) | 2020.05.05 |
---|---|
[백준][Python][10026][BFS] 적록색약 (0) | 2020.05.01 |
[백준][Python][7562][BFS] 나이트의 이동 (0) | 2020.04.30 |
[백준][Python][2667][BFS] 단지번호붙이기 (0) | 2020.04.29 |
[백준][Python][1012][BFS] 유기농 배추 (0) | 2020.04.28 |