File tree Expand file tree Collapse file tree 1 file changed +61
-0
lines changed
Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+
7+ static class State {
8+ int cur, clip;
9+
10+ State (int cur , int clip ) {
11+ this . cur = cur;
12+ this . clip = clip;
13+ }
14+ }
15+
16+ static int s;
17+ static int range = 2000 ;
18+
19+ public static void main (String [] args ) throws Exception {
20+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
21+ s = Integer . parseInt(br. readLine());
22+ System . out. println(bfs());
23+ }
24+
25+ static int bfs () {
26+ int [][] dist = new int [range + 1 ][range + 1 ];
27+ for (int i = 0 ; i <= range; i++ ) Arrays . fill(dist[i], - 1 );
28+
29+ ArrayDeque<State > q = new ArrayDeque<> ();
30+ q. add(new State (1 , 0 ));
31+ dist[1 ][0 ] = 0 ;
32+
33+ while (! q. isEmpty()) {
34+ State now = q. poll();
35+ int cur = now. cur;
36+ int clip = now. clip;
37+ int t = dist[cur][clip];
38+
39+ if (cur == s)
40+ return t;
41+
42+ if (dist[cur][cur] == - 1 ) {
43+ dist[cur][cur] = t + 1 ;
44+ q. add(new State (cur, cur));
45+ }
46+
47+ if (clip > 0 && cur + clip <= range && dist[cur + clip][clip] == - 1 ) {
48+ dist[cur + clip][clip] = t + 1 ;
49+ q. add(new State (cur + clip, clip));
50+ }
51+
52+ if (cur > 0 && dist[cur - 1 ][clip] == - 1 ) {
53+ dist[cur - 1 ][clip] = t + 1 ;
54+ q. add(new State (cur - 1 , clip));
55+ }
56+ }
57+ return - 1 ;
58+ }
59+ }
60+
61+ ```
You can’t perform that action at this time.
0 commit comments