Skip to content

Conversation

@YoonYn9915
Copy link
Member

@YoonYn9915 YoonYn9915 commented Jun 14, 2025

🌱WIL

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

  • 이번 한 주는 숨바꼭질 문제 시리즈를 풀어봤다. dp나 그래프탐색에 추가적인 조건(이번 문제에서는 '최소 시간 안으로 동생에게 도달하는 경로 횟수')이 들어가는 문제는 예전에 몇 번 풀어봤었고 경로에 대한 배열을 따로 만들어서 진행하는 것 까지는 했었는데 경로 누적, 경로 경신하는 로직에서 문제가 있어서 못 풀었던 것 같다. 스스로 좀 실망했다 ㅜㅜ.

🚀주간 목표 문제 수: 3개

푼 문제


백준 #2851. 숨바꼭질2: 그래프 / 골드4

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

🚩제출한 코드

import sys
from collections import deque

inp = sys.stdin.readline

N, K = map(int, inp().strip().split())
# 해당 위치로 도달한 최소 시간 저장
visited = [-1] * (100_000 + 1)
visited[N] = 0

# 최소시간에 해당 위치로 도달한 횟수 저장
ways = [-1] * (100_000 + 1)
ways[N] = 1

# (위치, 시간) 형식
queue = deque()
queue.append((N,0))

dx = [-1, 1, 2]

min_time = -1

while queue:
    subin_loc,time = queue.popleft()

    # 수빈이가 동생에게 도달한 시간까지만 bfs탐색하고 그 후에 종료
    if min_time != -1 and min_time +2 == time:
        break

    # 수빈이가 동생에게 도달한 시간체크
    if min_time == -1 and subin_loc == K:
        min_time = visited[subin_loc]


    # 수빈의 위치에서 3가지 이동
    for i in range(3):
        if i == 2:
            new_loc = subin_loc * dx[i]
        else:
            new_loc = subin_loc + dx[i]

        # 새 위치가 범위 안
        if 0 <= new_loc <= 100_000:
            # 새로 방문한 위치가 이전에 와보지 못했다면,
            if visited[new_loc] == -1:
                # 방문 시간 설정해주고 경로 초기화
                visited[new_loc] = visited[subin_loc] + 1
                ways[new_loc] = ways[subin_loc]
                queue.append((new_loc, time +1))

            # 새로 방문한 위치가 이전에 와봤다면, 지금 시간이 이전에 와봤던 시간과 같아야 함.
            else:
                if visited[new_loc] == visited[subin_loc] + 1:
                    # 경로 누적시켜줌
                    ways[new_loc] = ways[new_loc] + ways[subin_loc]


print(visited[K])
print(ways[K])

💡TIL

배운 점이 있다면 입력해주세요


백준 #1697. 숨바꼭질: 그래프 / 실버1

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

🚩제출한 코드

import sys
from collections import deque

inp = sys.stdin.readline

N, K = map(int, inp().strip().split())
# 해당 위치로 도달한 최소 시간 저장
visited = [-1] * (100_000 + 1)
visited[N] = 0

# (위치, 시간) 형식
queue = deque()
queue.append(N)

dx = [-1, 1, 2]

while queue:
    loc = queue.popleft()

    # 수빈이가 동생에게 도착했다면 종료
    if loc == K:
        print(visited[loc])
        break

    # 수빈의 위치에서 3가지 이동
    for i in range(3):
        if i == 2:
            new_loc = loc * dx[i]
        else:
            new_loc = loc + dx[i]

        # 새 위치가 범위 안이고 아직 방문하지 않았다면
        if 0 <= new_loc <= 100_000 and visited[new_loc] == -1:
            queue.append(new_loc)
            visited[new_loc] = visited[loc] + 1

💡TIL

배운 점이 있다면 입력해주세요


백준 #25206. 너의 평점은: 구현 / 실버5

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

🚩제출한 코드

rating = ['A+', 'A0', 'B+', 'B0', 'C+', 'C0', 'D+', 'D0', 'F']
grade = [4.5, 4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0]

total = 0
result = 0
for _ in range(20):
    s, p, g = input().split()
    p = float(p)
    if g != 'P':
        total += p
        result += p * grade[rating.index(g)]

print('%.6f' % (result / total))

💡TIL

배운 점이 있다면 입력해주세요

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.

한 주간 고생 많으셨습니다! 이번 문제는 BFS를 연습하기 위해 가져온 문제들인데요, 도움이 되었으면 좋겠습니다!

Copy link
Collaborator

Choose a reason for hiding this comment

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

이렇게 리스트로 받아두는 것도 좋은 방식인 것 같습니다!

Copy link
Collaborator

Choose a reason for hiding this comment

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

이 부분 줄여써도 좋을 것 같아요!
ways[new_loc] += ways[subin_loc]

@YoonYn9915 YoonYn9915 merged commit a93e43e into main Jun 21, 2025
@github-actions
Copy link

🔥2025-06 챌린지 진행 상황

👉 그래프

  • YoonYn9915: 3개 ❌
  • Mingguriguri: 2개 ❌

👉 구현

  • YoonYn9915: 1개 ❌
  • Mingguriguri: 1개 ❌

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.

3 participants