-
Notifications
You must be signed in to change notification settings - Fork 0
/
FalseMirrors.kt
83 lines (56 loc) · 1.72 KB
/
FalseMirrors.kt
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
70
71
72
73
74
75
76
77
78
79
import java.util.Arrays
fun main(args: Array<String>) {
var cnt = readLine()!!.toInt();
val values = readLine()!!.split(' ').map({ it.toInt() }).toIntArray();
var mask = 0;
var sum = 0;
for (i in 0..cnt-1) {
// println(">> "+(1 shl i).toString(2));
mask = mask or (1 shl i);
sum += values[i];
}
val cache = mutableMapOf<Int, Int>();
println(recurse(values, mask, sum, cache));
}
fun recurse(values:IntArray, m:Int, totalMonsters:Int, cache: MutableMap<Int,Int>):Int {
//println(m.toString(2)+" "+totalMonsters.toString());
var mask = m;
if (mask == 0) {
return 0;
}
if (cache.containsKey(mask)) {
return cache.getOrDefault(mask,0);
}
var minDamage = Int.MAX_VALUE;
for (i in 0..values.size-1) {
if (0 == (mask and ( 1 shl i )))
continue;
val killed = killMonsters(values, i, mask);
//println("Killed"+killed.toString())
val mask_copy = mask;
mask = destroy(values, i, mask);
val potentialDamage = totalMonsters - killed + recurse(values, mask, totalMonsters - killed, cache);
minDamage = Math.min(minDamage, potentialDamage)
mask = mask_copy;
}
cache.put(mask, minDamage);
return minDamage;
}
fun killMonsters(values:IntArray, n:Int, mask:Int):Int
{
var sum = 0;
for (i in 0..2) {
if ((mask and ( 1 shl ((n+i) % values.size) )) > 0) {
sum += values[(n+i) % values.size];
}
}
return sum
}
fun destroy(values:IntArray, n:Int, m:Int ):Int
{
var mask = m;
for (i in 0..2) {
mask = mask and ( 1 shl ((n+i) % values.size) ).inv();
}
return mask
}