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
31 changes: 31 additions & 0 deletions _WeeklyChallenges/W27-[BFS]/Assignment_BOJ_13549_숨바꼭질 3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from collections import deque
import sys
input = sys.stdin.readline

def bfs(i,j):
q = deque([i])
visited[i] = 1
while q:
x = q.popleft()
if x == j:
return
# !!NOTE!! 2*x, x-1, x+1 순서로 해야함, 그러지 않으면 틀림
for nx in (2*x, x-1, x+1):
if nx < 0 or nx > MAX:
continue
if visited[nx]:
continue

if nx == 2*x:
visited[nx] = visited[x]
else:
visited[nx] = visited[x] + 1

q.append(nx)


N, K = map(int, input().split())
MAX = 10**5
visited = [0] * (MAX+1)
bfs(N,K)
print(visited[K]-1)
16 changes: 16 additions & 0 deletions _WeeklyChallenges/W27-[BFS]/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## 🚀6월 3주차 (6/16) 스터디 발제 주제: BFS
> 발제자: 조윤상 (@YoonYn9915)

> [!NOTE]
> 주제: BFS

### 🗂️ 스터디 자료
- PDF: [바로가기](Study_BOJ_14226.pdf)

### 📖 문제
- [백준 #이모티콘](https://www.acmicpc.net/problem/14226): BFS / 골드 5
- 정답 코드: [Study_BOJ_14226_이모티콘.py](Study_BOJ_14226_이모티콘.py)

### 💻 과제
- [백준 #숨바꼭질 3](https://www.acmicpc.net/problem/13549): BFS / 골드 4
- 정답 코드: [Assignment_BOJ_13549_숨바꼭질 3.py](Assignment_BOJ_13549_숨바꼭질 3.py)
Binary file added _WeeklyChallenges/W27-[BFS]/Study_BOJ_14226.pdf
Binary file not shown.
34 changes: 34 additions & 0 deletions _WeeklyChallenges/W27-[BFS]/Study_BOJ_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)]

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
queue.append((screen - 1, clipboard))
Loading