forked from MiYazJE/Acepta-el-reto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p253.java
69 lines (52 loc) · 1.75 KB
/
p253.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
67
68
69
import java.util.*;
class S implements Comparable<S> {
int node, weight;
public S(int f, int w) {
this.node = f;
this.weight = w;
}
@Override
public int compareTo(S s) {
return this.weight - s.weight;
}
}
public class p253 {
static int minSteps(int target, HashMap<Integer, Integer> connections, boolean[] vis, int K, int N) {
PriorityQueue<S> q = new PriorityQueue<>();
q.add(new S(1, 0));
while (!q.isEmpty()) {
S current = q.poll();
for (int i = K; i >= 1; i--) {
if ((current.node + i) <= (N * N) && !vis[connections.get(current.node + i)]) {
int dest = connections.get(current.node + i);
vis[dest] = true;
if (dest == target)
return current.weight + 1;
q.add(new S(dest, current.weight + 1));
}
}
}
return -1;
}
public static void main(String[] args) {
final Scanner s = new Scanner(System.in);
int S, E, K, N;
HashMap<Integer, Integer> connections = new HashMap<>();
boolean[] vis = new boolean[10001];
while (true) {
N = s.nextInt();
K = s.nextInt();
S = s.nextInt();
E = s.nextInt();
if (N == 0 && K == 0 && S == 0 && E == 0)
break;
Arrays.fill(vis, false);
for (int i = 1; i <= N * N; i++)
connections.put(i, i);
for (int i = 0; i < S + E; i++)
connections.put(s.nextInt(), s.nextInt());
System.out.println( minSteps(N * N, connections, vis, K, N) );
connections.clear();
}
}
}