using
System;
using
System.Collections.Generic;
using
System.Collections;
class
Program
{
static
int
getMex(HashSet<
int
> s)
{
int
mex = 0;
while
(s.Contains(mex))
mex++;
return
mex;
}
static
int
checkOne(
int
mat,
int
i,
int
j,
int
k,
int
l)
{
var
m =
new
BitArray(
new
[] { mat });
for
(
int
x = i; x <= j; x++)
{
for
(
int
y = k; y <= l; y++)
{
int
pos = 15 - ((x * 4) + y);
if
(!m.Get(pos))
{
return
-1;
}
m.Set(pos,
false
);
}
}
var
res =
new
int
[1];
m.CopyTo(res, 0);
return
res[0];
}
static
int
getGrundy(
int
pos,
int
[] grundy)
{
if
(grundy[pos] != -1)
return
grundy[pos];
var
gSet =
new
HashSet<
int
>();
for
(
int
i = 0; i <= 3; i++)
{
for
(
int
j = i; j <= 3; j++)
{
for
(
int
k = 0; k <= 3; k++)
{
for
(
int
l = k; l <= 3; l++)
{
int
res = checkOne(pos, i, j, k, l);
if
(res != -1)
{
grundy[res] = getGrundy(res, grundy);
gSet.Add(grundy[res]);
}
}
}
}
}
return
getMex(gSet);
}
static
int
toInt(
int
[,] matrix)
{
int
h = 0;
for
(
int
i = 0; i < 4; ++i)
{
for
(
int
j = 0; j < 4; ++j)
{
h = 2 * h + matrix[i, j];
}
}
return
h;
}
static
void
Main(
string
[] args)
{
int
[,] mat = { { 0, 1, 1, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 1 } };
int
intValue = toInt(mat);
int
[] grundy =
new
int
[intValue + 1];
for
(
int
i = 0; i < grundy.Length; i++)
{
grundy[i] = -1;
}
grundy[0] = 0;
if
(getGrundy(intValue, grundy) != 0)
{
Console.WriteLine(
"Player A wins"
);
}
else
{
Console.WriteLine(
"Player B wins"
);
}
}
}