Skip to content

Commit aeafd0c

Browse files
author
hangyeol
committed
98차 1번 문제풀이
1 parent ef7a0c0 commit aeafd0c

1 file changed

Lines changed: 14 additions & 26 deletions

File tree

live9/test98/문제1/백한결.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,38 @@
22

33
def main():
44
N, M = map(int, input().split())
5-
a = list(map(int, input().strip().split()))[:N]
6-
5+
a = list(map(int, input().split()))
76
print(bfs(N, M, a))
87

9-
108
def bfs(N, M, a):
119
queue = deque()
1210

1311
# 시작위치 0, 눈덩이 크기 1, 시간 0초
14-
queue.append((0, 1, 0))
15-
16-
visited = set()
17-
visited.add((0, 1, 0))
18-
12+
queue.append((-1, 1, 0))
1913
maxSize = 1
2014

2115
while queue:
2216
location, size, time = queue.popleft()
2317

24-
maxSize = max(maxSize, size)
25-
26-
if time == M:
18+
# 시간 초과 or 마지막 칸 도달
19+
if time == M or location == N - 1:
20+
maxSize = max(maxSize, size)
2721
continue
2822

2923
# 눈동이를 한 칸 이동시킬 경우
30-
nextLocation = location + 1
31-
if nextLocation < N:
32-
plusSize = size + a[nextLocation]
33-
current = (nextLocation, plusSize, time+1)
34-
if current not in visited:
35-
visited.add(current)
36-
queue.append(current)
24+
nextLoc1 = location + 1
25+
if nextLoc1 < N:
26+
nextSize1 = size + a[nextLoc1]
27+
queue.append((nextLoc1, nextSize1, time + 1))
3728

3829
# 눈덩이를 두 칸 이동시킬 경우
39-
nextLocation = location + 2
40-
if nextLocation < N:
41-
plusSize = size // 2 + a[nextLocation]
42-
current = (nextLocation, plusSize, time+1)
43-
if current not in visited:
44-
visited.add(current)
45-
queue.append(current)
30+
nextLoc2 = location + 2
31+
if nextLoc2 < N:
32+
nextSize2 = size // 2 + a[nextLoc2]
33+
queue.append((nextLoc2, nextSize2, time + 1))
4634

47-
return maxSize
4835

36+
return maxSize
4937

5038
if __name__ == '__main__':
5139
main()

0 commit comments

Comments
 (0)