public
Created

Check whether Bob can force a zero pattern against Alice in a game of determinants; see

  • Download Gist
Question216503.java
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
 
public class Question216503 {
final static int n = 5;
final static int [] weights = {0,1,n+1};
 
static class State {
int ntypes [] = {n*n,0,0};
 
byte [] [] matrix = new byte [n] [n];
int [] [] order = new int [2] [n];
int [] [] sums = new int [2] [n];
 
{
for (int i = 0;i < n;i++)
order [0] [i] = order [1] [i] = i;
}
 
public boolean isValid () {
int diff = ntypes [1] - ntypes [2];
return diff == 1 || diff == 0;
}
 
public long toCode () {
long code = 0;
for (int i = 0;i < n;i++)
for (int j = 0;j < n;j++) {
code <<= 2;
code += matrix [order [0] [i]] [order [1] [j]];
}
return code;
}
 
public long toCode (int [] rows,int [] cols) {
long code = 0;
for (int i = 0;i < n;i++)
for (int j = 0;j < n;j++) {
code <<= 2;
code += matrix [order [0] [rows [i]]] [order [1] [cols [j]]];
}
return code;
}
 
public void fromCode (long code) {
for (int i = 0,shift = 2 * (n * n - 1);i < n;i++)
for (int j = 0;j < n;j++,shift -= 2)
set (i,j,(int) ((code >> shift) & 3));
}
 
public byte get (int i,int j) {
return matrix [i] [j];
}
 
public void set (int i,int j,int value) {
ntypes [matrix [i] [j]]--;
ntypes [value]++;
 
int change = weights [value] - weights [matrix [i] [j]];
matrix [i] [j] = (byte) value;
sums [0] [i] += change;
sums [1] [j] += change;
 
if (reordering)
fix (i,j);
}
 
private int rank (int i,int dir) {
for (int j = 0;j < n;j++)
if (order [dir] [j] == i)
return j;
throw new Error ();
}
 
private int [] r = new int [2];
 
final static boolean testing = false;
final static boolean reordering = true;
 
private void fix (int i,int j) {
r [0] = rank (i,0);
r [1] = rank (j,1);
 
for (int k = 0;k < 2;k++) {
while (r [k] != 0 && sums [k] [order [k] [r [k]]] > sums [k] [order [k] [r [k] - 1]]) {
int tmp = order [k] [r [k]];
order [k] [r [k]] = order [k] [r [k] - 1];
order [k] [r [k] - 1] = tmp;
r [k]--;
}
 
while (r [k] != n - 1 && sums [k] [order [k] [r [k]]] < sums [k] [order [k] [r [k] + 1]]) {
int tmp = order [k] [r [k]];
order [k] [r [k]] = order [k] [r [k] + 1];
order [k] [r [k] + 1] = tmp;
r [k]++;
}
}
 
if (testing)
check ();
}
 
private void check () {
for (int k = 0;k < 2;k++) {
boolean [] done = new boolean [n];
for (int l = 0;l < n;l++) {
if (done [order [k] [l]])
throw new Error ();
done [order [k] [l]] = true;
if (l != 0 && sums [k] [order [k] [l]] > sums [k] [order [k] [l - 1]])
throw new Error ();
if (l != n - 1 && sums [k] [order [k] [l]] < sums [k] [order [k] [l + 1]])
throw new Error ();
}
}
}
 
public String toString () {
StringBuilder builder = new StringBuilder ();
builder.append ('\n');
for (int i = 0;i < n;i++) {
for (int j = 0;j < n;j++) {
if (j != 0)
builder.append (' ');
builder.append (get (i,j));
}
builder.append ('\n');
}
return builder.toString ();
}
}
 
static Map<Long,Boolean> valueMap = new HashMap<Long,Boolean> ();
static List [] lists = {new ArrayList (),new ArrayList ()};
static int [] [] permutations = Permutations.getPermutations (n);
 
static boolean put (State state,boolean result) {
if (valueMap.put (state.toCode (),result) != null)
return true;
 
for (int k = 0;k < 2;k++) {
List<int []> list = (List<int []>) lists [k];
list.clear ();
outer:
for (int [] permutation : permutations) {
for (int i = 0;i < n;i++)
if (state.sums [k] [state.order [k] [i]] != state.sums [k] [state.order [k] [permutation [i]]])
continue outer;
list.add (permutation);
}
}
 
for (int [] p0 : (List<int []>) lists [0])
for (int [] p1 : (List<int []>) lists [1])
valueMap.put (state.toCode (p0,p1),result);
 
return false;
}
 
static void add (State state) {
add (state,new int [2]);
}
 
static void add (State state,int [] index) {
if (state.isValid () && put (state,false))
return;
 
int type = state.ntypes [1] > state.ntypes [2] ? 2 : 1;
int k = type - 1;
int was = index [k];
 
for (;index [k] < n * n;index [k]++) {
int i = index [k] % n;
int j = index [k] / n;
 
if (state.get (i,j) == 0) {
state.set (i,j,type);
add (state,index);
state.set (i,j,0);
}
}
 
index [k] = was;
}
 
static boolean play (State state,boolean first) {
Boolean known = valueMap.get (state.toCode ());
if (known != null)
return known;
 
boolean result = compute (state,first);
put (state,result);
return result;
}
 
static boolean compute (State state,boolean first) {
if (state.ntypes [0] == 0)
return true;
 
for (int i = 0;i < n;i++)
for (int j = 0;j < n;j++)
if (state.get (i,j) == 0) {
state.set (i,j,first ? 1 : 2);
boolean result = play (state,!first);
state.set (i,j,0);
 
if (result == first)
return first;
}
 
return !first;
}
 
public static void main (String [] args) {
for (int k = 1;k <= n;k++) {
State state = new State ();
for (int i = 0;i < k;i++)
for (int j = 0;j < n + 1 - k;j++)
state.set (i,j,2);
add (state);
}
 
State state = new State ();
state.set (0,0,1);
System.out.println ("value : " + play (state,false));
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.