반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
Tags
- 파이썬데이터분석라이브러리
- 백준 평범한배낭
- 1987파이썬
- 백준 점프
- 백준 전쟁 파이썬
- 등수매기기 파이썬
- MongoDB
- 백준 전쟁-전투
- 금고털이 파이썬
- 파이썬 평범한배낭
- MySQL완전삭제
- 소프티어 지도자동구축
- 백준 피아노체조
- jenkins
- 지도자동구축 파이썬
- 프로그래머스
- 피아노체조 파이썬
- 소프티어 장애물인식프로그램
- 백준 등수매기기
- 백준알파벳파이썬
- express
- 백준 예산
- 장애물인식프로그램 파이썬
- 백준
- 백준 A->B
- 백준 바이러스
- 도커 컨테이너
- 백준 점프 파이썬
- express mongodb
- CRUD
Archives
- Today
- Total
바위 뚫는중
[BOJ] 백준 2583. 영역 구하기 - BFS 본문
반응형
🥈영역 구하기
https://www.acmicpc.net/problem/2583
💡아이디어
📝 풀이
좌표를 입력받고 x2 - x1, y2- y1 만큼 해당 칸을 채워주고,
채워져 있지 않은 곳의 크기를 재주면 될 것 같다
입력받기
m, n, k = map(int, input().split())
graph = [[0] * n for _ in range(m)] # 가로 n 만큼의 세로 m 을 0 으로 채워주기
for _ in range(k):
x1, y1, x2, y2 = map(int, input().split()) #꼭지점입력받기
for i in range(x1,x2): #가로길이만큼
for j in range(m-y1-1, m-y2,-1, -1):
graph[j][i] = 1
알고리즘
dx = [-1,1,0,0]
dy = [0,0,-1,1]
def bfs(x,y):
q = deque()
q.append((x,y))
graph[x][y] = 1 # 방문처리 해준다
size = 1 #체크할 너비
while q:
x, y = q.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0<=nx<m and 0<=ny<n and graph[nx][ny]==0:
size += 1
graph[nx][ny] = 1
q.append((nx,ny))
ans.append(size)
ans = []
for i in range(m):
for j in range(n):
if graph[i][j] == 0:
bfs(i,j)
출력하기
ans.sort()
print(len(ans))
for i in ans:
print(i, end=' ')
전체코드
from collections import deque
m, n, k = map(int, input().split())
graph = [[0] * n for _ in range(m)] # 가로 n 만큼의 세로 m 을 0 으로 채워주기
for _ in range(k):
x1, y1, x2, y2 = map(int, input().split()) #꼭지점입력받기
for i in range(x1,x2): #가로길이만큼
for j in range(m-y1-1, m-y2-1, -1):
graph[j][i] = 1
dx = [-1,1,0,0]
dy = [0,0,-1,1]
def bfs(x,y):
q = deque()
q.append((x,y))
graph[x][y] = 1 # 방문처리 해준다
size = 1 #체크할 너비
while q:
x, y = q.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0<=nx<m and 0<=ny<n and graph[nx][ny]==0:
size += 1
graph[nx][ny] = 1
q.append((nx,ny))
ans.append(size)
ans = []
for i in range(m):
for j in range(n):
if graph[i][j] == 0:
bfs(i,j)
ans.sort()
print(len(ans))
for i in ans:
print(i, end=' ')
반응형
'Algorithms > 백준' 카테고리의 다른 글
[BOJ] 백준 20920. 영단어 암기는 괴로워 - 문자열 (0) | 2023.10.21 |
---|---|
[BOJ] 백준 7568. 덩치 - 구현 (1) | 2023.10.20 |
[BOJ] 백준 1244. 스위치 켜고 끄기 - 시뮬레이션 (1) | 2023.10.14 |
[BOJ] 백준 2563. 색종이 - 구현 (0) | 2023.10.13 |
[BOJ] 백준 1074. Z - 분할정복, 재귀 (0) | 2023.10.11 |