|
| 1 | +# 점프 |
| 2 | + |
| 3 | +# DP |
| 4 | +import sys |
| 5 | +input = sys.stdin.readline |
| 6 | + |
| 7 | +N, M = map(int, input().split()) |
| 8 | +dp = [[float('inf')] * (int((2 * N) ** 0.5) + 2) for _ in range(N + 1)] |
| 9 | +dp[1][0] = 0 |
| 10 | + |
| 11 | +stone_set = set() |
| 12 | +for _ in range(M): |
| 13 | + stone_set.add(int(input())) |
| 14 | +for i in range(2, N + 1): |
| 15 | + if i in stone_set: |
| 16 | + continue |
| 17 | + for j in range(1, int((2 * i) ** 0.5) + 1): |
| 18 | + dp[i][j] = min(dp[i - j][j - 1], dp[i - j][j], dp[i - j][j + 1]) + 1 |
| 19 | + |
| 20 | + |
| 21 | +if min(dp[N]) == float('inf'): |
| 22 | + print(-1) |
| 23 | +else: |
| 24 | + print(min(dp[N])) |
| 25 | + |
| 26 | +# BFS |
| 27 | +# import sys |
| 28 | +# from collections import deque |
| 29 | + |
| 30 | +# N, M = map(int, sys.stdin.readline().split()) |
| 31 | +# check = [[] for _ in range(N + 1)] |
| 32 | +# small_rock = set() |
| 33 | +# for _ in range(M): |
| 34 | +# small = int(sys.stdin.readline()) |
| 35 | +# small_rock.add(small) |
| 36 | + |
| 37 | + |
| 38 | +# def solution(N, check, small_rock): |
| 39 | +# queue = deque([(1, 0, 0)]) |
| 40 | +# while queue: |
| 41 | +# location, jump, n = queue.popleft() |
| 42 | +# for x in [jump + 1, jump, jump - 1]: |
| 43 | +# if x > 0: |
| 44 | +# next_location = location + x |
| 45 | +# if next_location == N: |
| 46 | +# return n + 1 |
| 47 | +# if next_location < N and next_location not in small_rock and x not in check[next_location]: |
| 48 | +# check[next_location].append(x) |
| 49 | +# queue.append((next_location, x, n + 1)) |
| 50 | +# return -1 |
| 51 | + |
| 52 | + |
| 53 | +# print(solution(N, check, small_rock)) |
0 commit comments