-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.java
66 lines (54 loc) Β· 1.63 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package BFS.P14226;
import java.util.*;
public class Main {
static int S;
static Queue<State> q = new LinkedList<>();
static boolean[][] visited = new boolean[10001][10001];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
S = sc.nextInt();
int ans = 0;
q.offer(new State(1, 0, 0));
while (!q.isEmpty()) {
State s = q.poll();
visited[s.screen][s.clip] = true;
if (s.screen == S) {
ans = s.time;
break;
} else if (!visited[s.screen][s.screen]) {
q.add(new State(s.screen, s.screen, s.time + 1));
}
if (s.screen + s.clip == S) {
ans = s.time + 1;
break;
} else if (s.clip != 0 && !visited[s.screen + s.clip][s.clip]) {
q.add(new State(s.screen + s.clip, s.clip, s.time + 1));
}
if (s.screen - 1 == S) {
ans = s.time + 1;
break;
} else if (s.screen >= 1 && !visited[s.screen - 1][s.clip]) {
q.add(new State(s.screen - 1, s.clip, s.time + 1));
}
}
System.out.println(ans);
}
}
class State {
int screen;
int clip;
int time;
public State(int screen, int clip, int time) {
this.screen = screen;
this.clip = clip;
this.time = time;
}
@Override
public String toString() {
return "State{" +
"screen=" + screen +
", clip=" + clip +
", time=" + time +
'}';
}
}