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
- Brigthics를 이용한 분석
- Brightics
- Brigthics Studio
- 혼공
- Brightics를 이용한 분석
- 혼공머신
- 노코드AI
- 삼성SDS Brigthics
- 포스코 아카데미
- 데이터분석
- 삼성SDS
- 브라이틱스 서포터즈
- 데이터 분석
- 삼성SDS Brightics
- 포스코 청년
- 캐글
- 삼성 SDS
- 추천시스템
- 팀 분석
- 모델링
- Brightics Studio
- 삼성 SDS Brigthics
- 혼공학습단
- Brigthics
- 직원 이직률
- 직원 이직여부
- 혼공머신러닝딥러닝
- 개인 의료비 예측
- 브라이틱스
- 영상제작기
Archives
- Today
- Total
데이터사이언스 기록기📚
[백준/Python] 2667번(그래프, DFS, BFS)_단지 번호 붙이기 본문
📌문제 유형
그래프 이론, 그래프 탐색, BFS, DFS (실버1)
📌문제
📌나의 문제풀이
- DFS로 풀이
n = int(input())
maps = []
for _ in range(n):
maps.append(list(map(int,input())))
def dfs(x,y):
global cnt
dx = [0,0,1,-1]
dy = [1,-1,0,0]
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < n:
if maps[nx][ny] == 1:
maps[nx][ny] = 0
cnt += 1
dfs(nx,ny)
return cnt
li = []
for i in range(n):
for j in range(n):
if maps[i][j] == 1:
cnt = 1
maps[i][j] = 0
cnt = dfs(i,j)
li.append(cnt)
print(len(li))
li.sort()
for num in li:
print(num)
📌 다른사람의 문제풀이
- BFS 풀이
from collections import deque
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
def bfs(graph, a, b):
n = len(graph)
queue = deque()
queue.append((a, b))
graph[a][b] = 0
count = 1
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx < 0 or nx >= n or ny < 0 or ny >= n:
continue
if graph[nx][ny] == 1:
graph[nx][ny] = 0
queue.append((nx, ny))
count += 1
return count
n = int(input())
graph = []
for i in range(n):
graph.append(list(map(int, input())))
cnt = []
for i in range(n):
for j in range(n):
if graph[i][j] == 1:
cnt.append(bfs(graph, i, j))
cnt.sort()
print(len(cnt))
for i in range(len(cnt)):
print(cnt[i])
📌 리뷰
- '오름차순' 조건 확인하고 코드 작성하기
728x90
'Coding Test > 백준(Python)' 카테고리의 다른 글
[백준/Python] 17615번(그리디)_볼 모으기 (0) | 2023.05.09 |
---|---|
[백준/Python] 7562번(그래프, BFS)_나이트의 이동 (0) | 2023.05.08 |
[백준/Python] 1138번(구현)_한 줄로 서기 (0) | 2023.05.06 |
[백준/Python] 2583번(그래프, BFS, DFS)_영역 구하기 (0) | 2023.04.28 |
[백준/Python] 1743번(그래프, DFS, BFS)_음식물 피하기 (0) | 2023.04.27 |
Comments