*문제 출처는 백준에 있습니다.
문제 제목: 영역 구하기 / 2583번 (실버 1단계)
문제 사이트: https://www.acmicpc.net/problem/2583
문제 설명

나의 풀이
from collections import deque
# 상 하 좌 우 움직임
move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
def solution():
n, m, k = map(int, input().split())
block = [[0] * m for _ in range(n)] # 블럭 생성
visited = [[False] * m for _ in range(n)] # 방문처리
result = [] # 결과 값
for _ in range(k):
nx, ny, ix, iy = map(int, input().split())
for y in range(ny, iy):
for x in range(nx, ix):
block[y][x] = 1
for i in range(n):
for j in range(m):
if block[i][j] == 0 and not visited[i][j]:
q = deque([(i, j)])
visited[i][j] = True
count = 1
while q:
iy, ix = q.popleft()
for ny, nx in move:
y = ny + iy
x = nx + ix
if 0 <= y < n and 0 <= x < m and not visited[y][x] and block[y][x] == 0:
q.append((y, x))
visited[y][x] = True
count += 1
result.append(count)
print(len(result))
result.sort()
for i in result:
print(i, end = ' ')
solution()

※ 알아야 할 것
for _ in range(k):
nx, ny, ix, iy = map(int, input().split())
for y in range(ny, iy):
for x in range(nx, ix):
block[y][x] = 1
블럭을 채우는 것이 중요합니다!!
'Coding Test > 백준-Python' 카테고리의 다른 글
| 백준 / 최소비용 구하기 2 / 11779번 / Python (0) | 2025.04.28 |
|---|---|
| 백준 / 수 묶기 / 1744번 / Python (0) | 2025.04.25 |
| 백준 / 제곱수 찾기 / 1025번 / Python (0) | 2025.04.21 |
| 백준 / N과 M (10) / 15664번 / Python (0) | 2025.04.17 |
| 백준 / 숨바꼭질 3 / 13549번 / Python (0) | 2025.04.14 |