-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUva12160.java
More file actions
54 lines (52 loc) · 1.76 KB
/
Copy pathUva12160.java
File metadata and controls
54 lines (52 loc) · 1.76 KB
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
import java.util.*;
import java.io.*;
/*
* Uva 12160 - Unlock the Lock
* @ author Mostafa Kamel
*/
public class Uva12160
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(br.readLine());
int L = Integer.parseInt(st.nextToken());
int U = Integer.parseInt(st.nextToken());
int R = Integer.parseInt(st.nextToken());
int Case = 1 ;
while(!(L==0&&U==0&&R==0))
{
st = new StringTokenizer(br.readLine());
int [] distTo = new int[10000];
int [] btn = new int[R];
for (int i = 0; i < R; i++)
btn[i] = Integer.parseInt(st.nextToken());
Queue<Integer> q = new LinkedList<>();
q.add(L);
distTo[L] = 0 ;
while (!q.isEmpty())
{
int curKey = q.remove();
for(int Key : btn)
{
int w = (Key+curKey) % 10000 ;
if(distTo[w] == 0)
{
q.add(w);
distTo[w] = distTo[curKey]+1 ;
}
}
}
sb.append("Case "+Case+++": ").append(distTo[U] == 0 ? "Permanently Locked":distTo[U]).append("\n");
st = new StringTokenizer(br.readLine());
L = Integer.parseInt(st.nextToken());
U = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());
}
pr.print(sb.toString());
pr.close();
br.close();
}
}