-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unlocker.java
51 lines (43 loc) · 1.07 KB
/
Unlocker.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
import java.util.ArrayList;
public class Unlocker
{
private Fridge fridge;
private final int limit = 6;
ArrayList<ArrayList<Integer>> allHistory = new ArrayList<>();
public void unlock (Fridge fridge) {
this.fridge = fridge;
unlocked (limit, new ArrayList<>());
print();
}
private void unlocked (int limit, ArrayList<Integer> history) {
if (fridge.isUnlock())
{
allHistory.add(history);
System.out.println("way");
return;
}
if (limit==0)
return;
// metka:
for (int i = 0; i < fridge.getCount(); i++) {
// for (Integer el: history) {
// if (el.intValue()==i) continue metka;
// }
if (history.contains(i)) continue;
fridge.turn(i);
history.add(i);
unlocked (limit-1, (ArrayList<Integer>) history.clone());
history.remove(history.size()-1);
fridge.turn(i);
}
}
private void print() {
if (allHistory.isEmpty()) {
System.out.println("Ways not found");
return;
}
for (ArrayList<Integer> hst : allHistory) {
System.out.println(hst.toString());
}
}
}