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 |
Tags
- 브라이틱스
- 팀 분석
- 추천시스템
- 삼성SDS Brightics
- Brigthics Studio
- 삼성 SDS Brigthics
- 직원 이직여부
- 직원 이직률
- Brigthics를 이용한 분석
- 데이터 분석
- 캐글
- Brigthics
- 포스코 아카데미
- Brightics Studio
- 개인 의료비 예측
- 혼공
- 데이터분석
- 브라이틱스 서포터즈
- 삼성 SDS
- 삼성SDS Brigthics
- Brightics를 이용한 분석
- 포스코 청년
- 삼성SDS
- 영상제작기
- 혼공학습단
- Brightics
- 혼공머신
- 모델링
- 노코드AI
- 혼공머신러닝딥러닝
Archives
- Today
- Total
데이터사이언스 기록기📚
[백준/Python] 2583번(그래프, BFS, DFS)_영역 구하기 본문
📌문제 유형
그래프 이론, 그래프 탐색, BFS, DFS (실버1)
📌문제
2583번: 영역 구하기
첫째 줄에 M과 N, 그리고 K가 빈칸을 사이에 두고 차례로 주어진다. M, N, K는 모두 100 이하의 자연수이다. 둘째 줄부터 K개의 줄에는 한 줄에 하나씩 직사각형의 왼쪽 아래 꼭짓점의 x, y좌표값과 오
www.acmicpc.net
📌나의 문제풀이
- BFS로 풀이
from collections import deque
m,n,k = map(int,input().split()) # n = x, m = y
maps = [[1] * m for _ in range(n)]
for _ in range(k):
x1, y1, x2, y2 = map(int,input().split())
for i in range(x1,x2):
for j in range(y1, y2):
maps[i][j] = 0
result = []
q = deque()
for x in range(n):
for y in range(m):
if maps[x][y] == 1:
ans = 0
q.append((x,y))
maps[x][y] = 0
while q:
x_,y_ = q.popleft()
ans += 1
dx = [0,0,1,-1]
dy = [1,-1,0,0]
for l in range(4):
nx = x_ + dx[l]
ny = y_ + dy[l]
if 0 <= nx < n and 0 <= ny < m:
if maps[nx][ny] == 1:
maps[nx][ny] = 0
q.append((nx,ny))
result.append(ans)
print(len(result))
result.sort()
print(*result)
📌 다른사람의 문제풀이
백준 알고리즘 2583번 영역 구하기(python)
기본적인 DFS & BFS 문제이다. DFS 풀이 import sys sys.setrecursionlimit(10000) def dfs(y, x, cnt): graph[y][x] = 1 for dy, dx in d: Y, X = y+dy, x+dx if (0
jinho-study.tistory.com
- BFS 풀이
from collections import deque
def bfs(i, j):
queue = deque()
queue.append((i, j))
d = [(-1, 0), (1, 0), (0, -1), (0, 1)]
cnt = 1
while queue:
y, x = queue.popleft()
for dy, dx in d:
Y, X = y+dy, x+dx
if (0 <= Y < M) and (0 <= X < N) and graph[Y][X] == 0:
graph[Y][X] = 1
queue.append((Y, X))
cnt += 1
return cnt
M, N, K = map(int, input().split())
graph = [[0]*N for _ in range(M)]
for _ in range(K):
x1, y1, x2, y2 = map(int, input().split())
for i in range(y1, y2):
for j in range(x1, x2):
graph[i][j] = 1
res = []
for i in range(M):
for j in range(N):
if graph[i][j] == 0:
graph[i][j] = 1
res.append(bfs(i, j))
print(len(res))
print(*sorted(res))
- DFS 풀이
import sys
sys.setrecursionlimit(10000)
def dfs(y, x, cnt):
graph[y][x] = 1
for dy, dx in d:
Y, X = y+dy, x+dx
if (0 <= Y < M) and (0 <= X < N) and graph[Y][X] == 0:
cnt = dfs(Y, X, cnt+1)
return cnt
M, N, K = map(int, input().split())
graph = [[0]*N for _ in range(M)]
for _ in range(K):
x1, y1, x2, y2 = map(int, input().split())
for i in range(y1, y2):
for j in range(x1, x2):
graph[i][j] = 1
d = [(-1, 0), (1, 0), (0, -1), (0, 1)]
res = []
for i in range(M):
for j in range(N):
if graph[i][j] == 0:
res.append(dfs(i, j, 1))
print(len(res))
print(*sorted(res))
📌 리뷰
- DFS 종료 조건 : cnt = dfs(x,y), 마지막에 return cnt로 간단히 해결하기
728x90
'Coding Test > 백준(Python)' 카테고리의 다른 글
[백준/Python] 2667번(그래프, DFS, BFS)_단지 번호 붙이기 (0) | 2023.05.07 |
---|---|
[백준/Python] 1138번(구현)_한 줄로 서기 (0) | 2023.05.06 |
[백준/Python] 1743번(그래프, DFS, BFS)_음식물 피하기 (0) | 2023.04.27 |
[백준/Python] 11403번(그래프 이론, 그래프 탐색, 플로이드-워셜)_경로찾기 (0) | 2023.04.26 |
[백준/Python] 1182번(브루트포스, 백트래킹)_부분수열의 합 (0) | 2023.04.26 |
Comments