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
34 changes: 34 additions & 0 deletions YoonYn9915/Graph/2025-06-16-[백준]-#14226-이모티콘.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from collections import deque
import sys

S = int(sys.stdin.readline().strip())

# visited[screen][clipboard]
visited = [[-1] * (S + 1) for _ in range(S + 1)]

Comment on lines +7 to +8
Copy link
Collaborator

Choose a reason for hiding this comment

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

아래에 if 문에서 범위만 잘 지정하면 visited 배열의 크기도 S+1만큼 생성할 수 있다는 것을 알게 되었어요. 저는 아래 조건들을 처리 못해서 결국 vistied 배열의 크기를 1001로 설정했거든요..ㅜ

Copy link
Member Author

Choose a reason for hiding this comment

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

붙여넣기에서 0부터 S까지인지 확인하는 조건만 달아주면 0부터 S까지 배열을 설정해도 괜찮다고 생각했습니다!

queue = deque()
queue.append((1, 0)) # 화면: 1, 클립보드: 0
visited[1][0] = 0

while queue:
screen, clipboard = queue.popleft()

# 목표 이모티콘 수에 도달하면 종료
if screen == S:
print(visited[screen][clipboard])
break

# 1. 복사 (화면 -> 클립보드)
if visited[screen][screen] == -1:
visited[screen][screen] = visited[screen][clipboard] + 1
queue.append((screen, screen))

# 2. 붙여넣기 (클립보드 -> 화면)
if clipboard != 0 and screen + clipboard <= S and visited[screen + clipboard][clipboard] == -1:
visited[screen + clipboard][clipboard] = visited[screen][clipboard] + 1
queue.append((screen + clipboard, clipboard))

# 3. 삭제 (화면 -1)
if screen - 1 >= 0 and visited[screen - 1][clipboard] == -1:
visited[screen - 1][clipboard] = visited[screen][clipboard] + 1
Comment on lines +21 to +33
Copy link
Collaborator

Choose a reason for hiding this comment

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

저는 for문을 썼는데, 윤상님 보니까 굳이 for문 쓸 필요가 없었네요!

Copy link
Member Author

Choose a reason for hiding this comment

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

3가지이기도 하고 반복문으로 묶을 이유가 없다고 생각했어요

queue.append((screen - 1, clipboard))
50 changes: 50 additions & 0 deletions YoonYn9915/Graph/2025-06-21-[백준]-#13549-숨바꼭질 3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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, 0))

dx = [2, -1, 1]

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]
Comment on lines +23 to +28
Copy link
Collaborator

Choose a reason for hiding this comment

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

혹시 여기 min_time의 역할은 무엇인가요?

Copy link
Member Author

Choose a reason for hiding this comment

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

사실 이전 숨바꼭질 2문제랑 유사하게 풀어가다가 필요없어서 지웠어야 했는데 놔둔거라서 없어도 되는 변수이긴 해요..


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

Comment on lines +31 to +36
Copy link
Collaborator

Choose a reason for hiding this comment

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

3가지 이동 방법(2*x , x-1 , x+1) 을 변화량 dx 와 인덱스 i 로 정리한 것이 인상적입니다.
민정님 말씀처럼 코드 확장할 때 용이 할 것 같네요!

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

Choose a reason for hiding this comment

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

i값에 따라 순간이동(0초), 걷기(1초)를 구분하신 것 같네요!
추후 확장할 때에는 좋은 코드인 것 같습니다.



print(visited[K])
30 changes: 30 additions & 0 deletions YoonYn9915/implementation/2025-06-21-[백준]-#10828-스택.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sys
input = sys.stdin.readline

N = int(input())

stack = []
def query():
Q = input().split()
if Q[0] == "push":
stack.append(int(Q[1]))
elif Q[0] == "pop":
if len(stack):
print(stack.pop())
else:
print(-1)
elif Q[0] == "size":
print(len(stack))
elif Q[0] == "empty":
if len(stack):
print(0)
else:
print(1)
elif Q[0] == "top":
if len(stack):
print(stack[-1])
else:
print(-1)

for _ in range(N):
query()