Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions minjeong/DFSBFS/2025-06-16-[백준]-#14226-이모티콘.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import sys
from collections import deque
input = sys.stdin.readline

# 1. 입력 및 초기화
S = int(input())
MAX = 1001

# visited[screen][clipboard] = 해당 상태까지 걸린 시간
visited = [[-1] * MAX for _ in range(MAX)]
visited[1][0] = 0

queue = deque([(1, 0)]) # 화면 임티 개수, 클립보드 임티 개수

# 2. BFS 탐색
while queue:
screen, clip = queue.popleft()

# 3. 목표 달성 시 종료
if screen == S:
print(visited[screen][clip])
break

for i in range(3):
if i == 0: # 복사 (화면 → 클립보드)
new_screen, new_clipboard = screen, screen
elif i == 1: # 붙여넣기 (클립보드 → 화면)
new_screen, new_clipboard = screen + clip, clip
else: # 삭제 (화면 - 1)
new_screen, new_clipboard = screen - 1, clip

if new_screen >= MAX or new_screen < 0 \
or new_clipboard >= MAX or new_clipboard < 0 \
or visited[new_screen][new_clipboard] != -1:
continue

visited[new_screen][new_clipboard] = visited[screen][clip] + 1
queue.append((new_screen, new_clipboard))
28 changes: 28 additions & 0 deletions minjeong/DFSBFS/2025-06-21-[백준]-#13549-숨바꼭질3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sys
from collections import deque
input = sys.stdin.readline

MAX = 100000
N, K = map(int, input().split())
visited = [-1] * (MAX + 1)

queue = deque()
queue.append(N)
visited[N] = 0

while queue:
current = queue.popleft()

# 순간이동 (0초) -> 큐 앞쪽에 넣기
nx = current * 2
if 0 <= nx <= MAX and visited[nx] == -1:
visited[nx] = visited[current]
queue.appendleft(nx) # 핵심!
Comment on lines +16 to +20
Copy link
Member

Choose a reason for hiding this comment

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

넵 이번 숨바꼭질 3문제는 숨바꼭질 2문제의 기본 뼈대에
0초에 일어나는 '순간이동' 동작은 다른 두 동작보다 우선순위가 높아(두 동작보다 빨리 끝나니까) 항상 순간이동한 좌표를 큐의 앞에 위치시키는 것이 핵심인 문제였습니다.
저는 순간이동을 처리할 때 그냥 for문안에 if문을 활용했는데 민정님처럼 appendleft()를 쓰거나 우선순위큐를 쓰면 더욱 가독성있는 코드가 될 것같습니다.


# 걷는 경우 (+1, -1) -> 큐 뒤쪽에 넣기
for nx in (current - 1, current + 1):
if 0 <= nx <= MAX and visited[nx] == -1:
visited[nx] = visited[current] + 1
queue.append(nx)

Copy link
Collaborator

Choose a reason for hiding this comment

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

저는 이번 문제는 다른 풀이 참고해서 아래와 같이 for 문 탐색순서를 우선순위 따라 나열했는데 ** 0-1BFS** 란 풀이 유형이라는 건 처음 알았네요.

풀이 유형을 정의하고, 그에 대해 가독성 좋게 정리해주셔서 덕분에 이해가 잘 됐습니다!

for nx in (2*cx , cx-1 , cx+1 ) :

print(visited[K])
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
def solution(numbers, hand):
answer = ''
pad = {'1': (0, 0), '2': (0, 1), '3': (0, 2),
'4': (1, 0), '5': (1, 1), '6': (1, 2),
'7': (2, 0), '8': (2, 1), '9': (2, 2),
'*': (3, 0), '0': (3, 1), '#': (3, 2)
}

left = pad['*']
right = pad['#']

for num in numbers:
# 왼손이 눌러야 하는 번호
if num in (1, 4, 7):
answer += 'L'
left = pad[str(num)]
# 오른손이 눌러야 하는 번호
elif num in (3, 6, 9):
answer += 'R'
right = pad[str(num)]
# 가운데 번호일 경우 (2, 5, 8, 0)
else:

# 해당 번호와 왼손 거리
left_dist = abs(left[0] - pad[str(num)][0]) + abs(left[1] - pad[str(num)][1])
# 해당 번호와 오른손 거리
right_dist = abs(right[0] - pad[str(num)][0]) + abs(right[1] - pad[str(num)][1])

# 더 가까운 거리
if left_dist < right_dist:
answer += 'L'
left = pad[str(num)]
elif left_dist > right_dist:
answer += 'R'
right = pad[str(num)]
# 왼손과 오른손 거리가 같을 경우
else:
if hand == 'right':
answer += 'R'
right = pad[str(num)]
else:
answer += 'L'
left = pad[str(num)]

return answer