백준 문제풀이
2346번: 풍선 터뜨리기 / silver 3 / 덱
RonLee
2023. 5. 9. 23:06
https://www.acmicpc.net/problem/2346
2346번: 풍선 터뜨리기
1번부터 N번까지 N개의 풍선이 원형으로 놓여 있고. i번 풍선의 오른쪽에는 i+1번 풍선이 있고, 왼쪽에는 i-1번 풍선이 있다. 단, 1번 풍선의 왼쪽에 N번 풍선이 있고, N번 풍선의 오른쪽에 1번 풍선
www.acmicpc.net
'''
2346번: 풍선 터뜨리기 / silver 3 / 덱
'''
import sys
input = sys.stdin.readline
# 입력받기
N = int(input())
arr = list(map(int, input().split()))
visited = [False for _ in range(N)]
now_index = 0
cnt = 0
while True:
# 터뜨리기
print(now_index + 1, end=' ')
visited[now_index] = True
cnt += 1
if cnt == N:
break
# 다음 풍선 인덱스 이동
move = arr[now_index]
tmp = 0
if move > 0:
plus = 1
else:
plus = -1
while tmp != move:
tmp += plus
now_index += plus
if now_index == -1:
now_index = len(arr) - 1
elif now_index == len(arr):
now_index = 0
while visited[now_index]:
now_index += plus
if now_index == -1:
now_index = len(arr) - 1
elif now_index == len(arr):
now_index = 0
코드 리뷰
- 인덱스를 하나하나씩 움직이는 과정들을 구현했습니다.
- move만큼 움직일때, 한 칸 움직이고 해당 풍선이 터졌던 인덱스면 움직임을 세지 않고 다음 칸으로 이동합니다.