-
Notifications
You must be signed in to change notification settings - Fork 0
/
12160 - Unlock the Lock
47 lines (46 loc) · 1.72 KB
/
12160 - Unlock the Lock
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
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static int L,U,R,Case = 1;
static ArrayList<Integer> buttons = new ArrayList<>();
static String line = "";
static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
// write your code here
while ((line=br.readLine())!=null&&!line.equals("0 0 0")){
StringTokenizer st = new StringTokenizer(line);
L = Integer.parseInt(st.nextToken());
U = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
buttons.clear();
while (st.hasMoreTokens())
buttons.add(Integer.parseInt(st.nextToken()));
Queue<Integer> queue = new LinkedList<>();
queue.add(L);
int [] distance = new int[10000];
while (!queue.isEmpty()){
int u= queue.poll();
for (int i=0;i<buttons.size();i++){
int v = (u+buttons.get(i))%10000;
if (distance[v]==0){
distance[v] = distance[u]+1;
queue.add(v);
}
}
}
if (L==R)
pw.println("Case "+(Case++)+": 0");
else if (distance[U]!=0){
pw.println("Case "+(Case++)+": "+distance[U]);
}
else
pw.println("Case "+(Case++)+": Permanently Locked");
}
pw.flush();
}
}