Skip to content

Commit 4125430

Browse files
authored
Merge pull request #1369 from AlgorithmWithGod/0224LJH
[20251110] BOJ / G4 / 점프 / 이종환
2 parents ad5a417 + 1f5a9df commit 4125430

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

0224LJH/202511/10 BOJ 점프.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Collections;
8+
import java.util.HashMap;
9+
import java.util.HashSet;
10+
import java.util.LinkedList;
11+
import java.util.PriorityQueue;
12+
import java.util.Queue;
13+
import java.util.StringTokenizer;
14+
15+
public class Main {
16+
17+
static int[][] dp = new int[10001][500];
18+
static HashSet<Integer> set = new HashSet<>();
19+
static int total,setSize,ans;
20+
21+
22+
public static void main(String[] args) throws IOException {
23+
init();
24+
process();
25+
print();
26+
27+
28+
}
29+
30+
private static void init() throws IOException{
31+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
32+
StringTokenizer st = new StringTokenizer(br.readLine());
33+
total = Integer.parseInt(st.nextToken());
34+
setSize = Integer.parseInt(st.nextToken());
35+
for (int i = 0; i < setSize; i++) {
36+
set.add(Integer.parseInt(br.readLine()));
37+
}
38+
for (int i = 0; i <= 10000; i++) {
39+
Arrays.fill(dp[i], Integer.MAX_VALUE);
40+
}
41+
42+
43+
}
44+
45+
private static void process() throws IOException {
46+
if (set.contains(2)) {
47+
ans = -1;
48+
return;
49+
}
50+
dp[2][1] = 1;
51+
for (int i = 2; i< total; i++) {
52+
if (set.contains(i)) continue;
53+
for (int j = 1; j < 500; j++) {
54+
if (dp[i][j] == Integer.MAX_VALUE) continue;
55+
56+
int more = j+1;
57+
int less =j-1;
58+
if (more < 500 && i+more <= total && !set.contains(i+more)) {
59+
dp[i+more][more] = Math.min(dp[i+more][more], dp[i][j]+1);
60+
}
61+
if (j < 500 && i+j <= total && !set.contains(i+j)) {
62+
dp[i+j][j] = Math.min(dp[i+j][j], dp[i][j]+1);
63+
}
64+
if (less != 0 && i+less <=total && !set.contains(i+less)) {
65+
dp[i+less][less] = Math.min(dp[i+less][less], dp[i][j]+1);
66+
}
67+
}
68+
}
69+
70+
ans = Integer.MAX_VALUE;
71+
for (int i = 1; i < 500; i++) {
72+
ans = Math.min(ans, dp[total][i]);
73+
}
74+
if (ans == Integer.MAX_VALUE) ans = -1;
75+
76+
}
77+
78+
79+
private static void print() {
80+
System.out.print(ans);
81+
}
82+
83+
}
84+
```

0 commit comments

Comments
 (0)