File tree Expand file tree Collapse file tree 3 files changed +114
-0
lines changed
Expand file tree Collapse file tree 3 files changed +114
-0
lines changed Original file line number Diff line number Diff line change 1+ from collections import deque
2+ import sys
3+
4+ S = int (sys .stdin .readline ().strip ())
5+
6+ # visited[screen][clipboard]
7+ visited = [[- 1 ] * (S + 1 ) for _ in range (S + 1 )]
8+
9+ queue = deque ()
10+ queue .append ((1 , 0 )) # 화면: 1, 클립보드: 0
11+ visited [1 ][0 ] = 0
12+
13+ while queue :
14+ screen , clipboard = queue .popleft ()
15+
16+ # 목표 이모티콘 수에 도달하면 종료
17+ if screen == S :
18+ print (visited [screen ][clipboard ])
19+ break
20+
21+ # 1. 복사 (화면 -> 클립보드)
22+ if visited [screen ][screen ] == - 1 :
23+ visited [screen ][screen ] = visited [screen ][clipboard ] + 1
24+ queue .append ((screen , screen ))
25+
26+ # 2. 붙여넣기 (클립보드 -> 화면)
27+ if clipboard != 0 and screen + clipboard <= S and visited [screen + clipboard ][clipboard ] == - 1 :
28+ visited [screen + clipboard ][clipboard ] = visited [screen ][clipboard ] + 1
29+ queue .append ((screen + clipboard , clipboard ))
30+
31+ # 3. 삭제 (화면 -1)
32+ if screen - 1 >= 0 and visited [screen - 1 ][clipboard ] == - 1 :
33+ visited [screen - 1 ][clipboard ] = visited [screen ][clipboard ] + 1
34+ queue .append ((screen - 1 , clipboard ))
Original file line number Diff line number Diff line change 1+ import sys
2+ from collections import deque
3+
4+ inp = sys .stdin .readline
5+
6+ N , K = map (int , inp ().strip ().split ())
7+ # 해당 위치로 도달한 최소 시간 저장
8+ visited = [- 1 ] * (100_000 + 1 )
9+ visited [N ] = 0
10+
11+ # (위치, 시간) 형식
12+ queue = deque ()
13+ queue .append ((N , 0 ))
14+
15+ dx = [2 , - 1 , 1 ]
16+
17+ min_time = - 1
18+
19+ while queue :
20+ subin_loc , time = queue .popleft ()
21+
22+ # 수빈이가 동생에게 도달한 시간까지만 bfs탐색하고 그 후에 종료
23+ if min_time != - 1 and min_time + 2 == time :
24+ break
25+
26+ # 수빈이가 동생에게 도달한 시간체크
27+ if min_time == - 1 and subin_loc == K :
28+ min_time = visited [subin_loc ]
29+
30+ # 수빈의 위치에서 3가지 이동
31+ for i in range (3 ):
32+ if i == 0 :
33+ new_loc = subin_loc * dx [i ]
34+ else :
35+ new_loc = subin_loc + dx [i ]
36+
37+ # 새 위치가 범위 안
38+ if 0 <= new_loc <= 100_000 :
39+ # 새로 방문한 위치가 이전에 와보지 못했다면,
40+ if visited [new_loc ] == - 1 :
41+ # 방문 시간 설정해주고 경로 초기화
42+ if i == 0 :
43+ visited [new_loc ] = visited [subin_loc ]
44+ queue .append ((new_loc , time ))
45+ else :
46+ visited [new_loc ] = visited [subin_loc ] + 1
47+ queue .append ((new_loc , time + 1 ))
48+
49+
50+ print (visited [K ])
Original file line number Diff line number Diff line change 1+ import sys
2+ input = sys .stdin .readline
3+
4+ N = int (input ())
5+
6+ stack = []
7+ def query ():
8+ Q = input ().split ()
9+ if Q [0 ] == "push" :
10+ stack .append (int (Q [1 ]))
11+ elif Q [0 ] == "pop" :
12+ if len (stack ):
13+ print (stack .pop ())
14+ else :
15+ print (- 1 )
16+ elif Q [0 ] == "size" :
17+ print (len (stack ))
18+ elif Q [0 ] == "empty" :
19+ if len (stack ):
20+ print (0 )
21+ else :
22+ print (1 )
23+ elif Q [0 ] == "top" :
24+ if len (stack ):
25+ print (stack [- 1 ])
26+ else :
27+ print (- 1 )
28+
29+ for _ in range (N ):
30+ query ()
You can’t perform that action at this time.
0 commit comments