-
Notifications
You must be signed in to change notification settings - Fork 5
YoonYn9915/ 6월 3주차/ 3문제 #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
Comment on lines
+21
to
+33
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 for문을 썼는데, 윤상님 보니까 굳이 for문 쓸 필요가 없었네요!
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3가지이기도 하고 반복문으로 묶을 이유가 없다고 생각했어요 |
||
| queue.append((screen - 1, clipboard)) | ||
| 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 혹시 여기
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i값에 따라 순간이동(0초), 걷기(1초)를 구분하신 것 같네요! |
||
|
|
||
|
|
||
| print(visited[K]) | ||
| 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() |
There was a problem hiding this comment.
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로 설정했거든요..ㅜ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
붙여넣기에서 0부터 S까지인지 확인하는 조건만 달아주면 0부터 S까지 배열을 설정해도 괜찮다고 생각했습니다!