Skip to content

Commit 15b0786

Browse files
authored
Merge pull request #226 from AlgorithmStudy-Allumbus/minjeong3
Minjeong / 6월 3주차 / 3문제
2 parents e85eb21 + ee610f2 commit 15b0786

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
5+
# 1. 입력 및 초기화
6+
S = int(input())
7+
MAX = 1001
8+
9+
# visited[screen][clipboard] = 해당 상태까지 걸린 시간
10+
visited = [[-1] * MAX for _ in range(MAX)]
11+
visited[1][0] = 0
12+
13+
queue = deque([(1, 0)]) # 화면 임티 개수, 클립보드 임티 개수
14+
15+
# 2. BFS 탐색
16+
while queue:
17+
screen, clip = queue.popleft()
18+
19+
# 3. 목표 달성 시 종료
20+
if screen == S:
21+
print(visited[screen][clip])
22+
break
23+
24+
for i in range(3):
25+
if i == 0: # 복사 (화면 → 클립보드)
26+
new_screen, new_clipboard = screen, screen
27+
elif i == 1: # 붙여넣기 (클립보드 → 화면)
28+
new_screen, new_clipboard = screen + clip, clip
29+
else: # 삭제 (화면 - 1)
30+
new_screen, new_clipboard = screen - 1, clip
31+
32+
if new_screen >= MAX or new_screen < 0 \
33+
or new_clipboard >= MAX or new_clipboard < 0 \
34+
or visited[new_screen][new_clipboard] != -1:
35+
continue
36+
37+
visited[new_screen][new_clipboard] = visited[screen][clip] + 1
38+
queue.append((new_screen, new_clipboard))
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
5+
MAX = 100000
6+
N, K = map(int, input().split())
7+
visited = [-1] * (MAX + 1)
8+
9+
queue = deque()
10+
queue.append(N)
11+
visited[N] = 0
12+
13+
while queue:
14+
current = queue.popleft()
15+
16+
# 순간이동 (0초) -> 큐 앞쪽에 넣기
17+
nx = current * 2
18+
if 0 <= nx <= MAX and visited[nx] == -1:
19+
visited[nx] = visited[current]
20+
queue.appendleft(nx) # 핵심!
21+
22+
# 걷는 경우 (+1, -1) -> 큐 뒤쪽에 넣기
23+
for nx in (current - 1, current + 1):
24+
if 0 <= nx <= MAX and visited[nx] == -1:
25+
visited[nx] = visited[current] + 1
26+
queue.append(nx)
27+
28+
print(visited[K])
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
def solution(numbers, hand):
2+
answer = ''
3+
pad = {'1': (0, 0), '2': (0, 1), '3': (0, 2),
4+
'4': (1, 0), '5': (1, 1), '6': (1, 2),
5+
'7': (2, 0), '8': (2, 1), '9': (2, 2),
6+
'*': (3, 0), '0': (3, 1), '#': (3, 2)
7+
}
8+
9+
left = pad['*']
10+
right = pad['#']
11+
12+
for num in numbers:
13+
# 왼손이 눌러야 하는 번호
14+
if num in (1, 4, 7):
15+
answer += 'L'
16+
left = pad[str(num)]
17+
# 오른손이 눌러야 하는 번호
18+
elif num in (3, 6, 9):
19+
answer += 'R'
20+
right = pad[str(num)]
21+
# 가운데 번호일 경우 (2, 5, 8, 0)
22+
else:
23+
24+
# 해당 번호와 왼손 거리
25+
left_dist = abs(left[0] - pad[str(num)][0]) + abs(left[1] - pad[str(num)][1])
26+
# 해당 번호와 오른손 거리
27+
right_dist = abs(right[0] - pad[str(num)][0]) + abs(right[1] - pad[str(num)][1])
28+
29+
# 더 가까운 거리
30+
if left_dist < right_dist:
31+
answer += 'L'
32+
left = pad[str(num)]
33+
elif left_dist > right_dist:
34+
answer += 'R'
35+
right = pad[str(num)]
36+
# 왼손과 오른손 거리가 같을 경우
37+
else:
38+
if hand == 'right':
39+
answer += 'R'
40+
right = pad[str(num)]
41+
else:
42+
answer += 'L'
43+
left = pad[str(num)]
44+
45+
return answer

0 commit comments

Comments
 (0)