Skip to content

Conversation

@zaqquum
Copy link
Collaborator

@zaqquum zaqquum commented Aug 15, 2025

🌱WIL

이번 한 주의 소감을 작성해주세요!

🚀주간 목표 문제 수: 4 개

푼 문제


백준 #5014. 스타트링크: 그래프 / 실버1

정리한 링크: (바로가기)

🚩플로우 (선택)

코드를 풀이할 때 적었던 플로우가 있나요?

  1. 총 0 ~f 층 방문 배열 생성(미방문 조건 -> 최단 거리 확보)
  2. BFS 진행
    • 변수 정의
      • qeue : [현 위치하는 층수 ]
      • 방문 여부 visited[층수] = 버튼 횟수(: -1)
    • 탐색 범위 : 1 <= {다음 탐색 층} <= f & 미방문 층

🚩제출한 코드

import sys
from collections import deque
input = sys.stdin.readline

total_floors , cp , tp , up , down = map(int, input().split())

def bfs(start, end , total_floors, up , down):
    building = [-1 for _ in range(total_floors+1)]
    #1. 시작 위치 start 의 초기화
    q = deque([start])
    building[start] = 0 
    #2. bfs 탐색
    while q :
        cn = q.popleft()
        cbutton = building[cn]
        # pruning 조건 :
        if cn == end : 
            break
        for dh in [up , -down] : # 엘베 2가지 조작 방법 :up , down
            nn = cn + dh 
            # 다음 층이 건물 층수 범위내에 존재함& 미방문=> 방문하기
            if 1 <= nn <= total_floors and building[nn] <0 : 
                q.append(nn)
                building[nn] = cbutton +1 
    return building[end]


answer=bfs(cp , tp , total_floors , up , down)
#3. 출력 형식 지정 
if answer < 0 : 
    print("use the stairs")
else :
    print(answer)

백준 #2644. 촌수계산: 그래프/ 실버2

정리한 링크: (바로가기)

🚩플로우 (선택)

코드를 풀이할 때 적었던 플로우가 있나요?

  1. 입력 변수 및 양방향 인접 리스트 nodes 정의 및 초기화
  2. 대상 부모 tx ↔ 자식 ty 까지의 최단 거리를 구하기 : BFS 사용
    • 촌수 = BFS 의 level
    • 큐 q = [tx ,0] # 대상 부모 tx 와 tx 와의 level 값을 할당
    • BFS 탐색 중 만약 대상 ty와 같을 경우 , 현재 level cn 로 정답 업데이트 후 탐색 종료
    • 현재 cn 과 직접적인 관계 가짐& 미방문한 번호만 q , visited 에 추가

🚩제출한 코드

import sys
from collections import deque
input = sys.stdin.readline

#1. 입력 변수 

N = int(input())
nodes = [[] for _ in range(N+1)]
tx , ty = map(int, input().split())

# 인접 리스트 nodes 만들기(양방향)
M = int(input())
for _ in range(M):
    x,y= map(int,input().split())
    nodes[x].append(y)
    nodes[y].append(x)

# 2.x -> y 의 최단 거리 찾기 : BFS
# 거리 = BFS의 level
q = deque([[tx , 0] ]) # 현재 번호 & level 정보를 queue 담기
visited = []
answer = -1
while q : 
    cn , cl = q.popleft() 
    if cn == ty : # target 값에 도달할때만 촌수를 answer에 업데이트 하기
        answer = cl 
        break
    for nn in nodes[cn]: # cn과 연결된 번호 & 미방문 -> 방문
        if nn not in visited : 
            visited.append(nn)
            q.append([nn ,cl+1] )

print(answer)

YoonYn9915 and others added 18 commits July 2, 2025 05:14
…ntation

6월 3주차 (6/16) 스터디 발제 자료 업로드
ttps://school.programmers.co.kr/learn/courses/30/lessons/42627
ttps://school.programmers.co.kr/learn/courses/30/lessons/42627
Copy link
Collaborator

@Mingguriguri Mingguriguri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이번 한 주도 좋은 문제 가져와주셔서 감사합니다! 재미있는 문제 풀이였어요!

Comment on lines +31 to +40
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 dfs로 풀긴 했는데 bfs로 풀어도 로직 자체는 비슷한 것 같네요!

Comment on lines +34 to +39
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2가지 조작법이라 전 각각 따로 처리하긴 했는데 for loop 도는 게 더 깔끔한 것 같네요!

@zaqquum zaqquum merged commit 4d8f75d into main Aug 18, 2025
@github-actions
Copy link

🔥2025-08 챌린지 진행 상황

👉 그래프

  • Mingguriguri: 2개 ❌
  • zaqquum: 2개 ❌

👉 구현

  • Mingguriguri: 2개 ❌
  • zaqquum: 0개 ❌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants