public static void Precalculate(long diamonds) { NumPoss[1] = 1; AllPos.Add(1, new Dictionary <BoardPos, double>()); BoardPos pos = new BoardPos(); pos.AddDiamond(0, 0); AllPos[1].Add(pos, 1); for (long diamond = 2; diamond <= diamonds; diamond++) { AllPos.Add(diamond, new Dictionary <BoardPos, double>()); foreach (var oldPos in AllPos[diamond - 1]) { var newPositions = GetNewPositions(oldPos); foreach (var newPos in newPositions) { if (!AllPos[diamond].ContainsKey(newPos)) { AllPos[diamond][newPos] = 0; } AllPos[diamond][newPos] += oldPos.Value / (double)newPositions.Count; } } } }
public static HashSet <BoardPos> GetNewPositions(Pos diamondPos, BoardPos oldPos) { HashSet <BoardPos> positions = new HashSet <BoardPos>(); if (diamondPos.Y == 0) { var newPos = new BoardPos(oldPos); newPos.Diamonds.Add(diamondPos); positions.Add(newPos); } else { Pos possiblePos; bool blockedBoth = true; possiblePos = new Pos(diamondPos.X - 1, diamondPos.Y - 1); if (!oldPos.Diamonds.Contains(possiblePos)) { blockedBoth = false; positions.UnionWith(GetNewPositions(possiblePos, oldPos)); } possiblePos = new Pos(diamondPos.X + 1, diamondPos.Y - 1); if (!oldPos.Diamonds.Contains(possiblePos)) { positions.UnionWith(GetNewPositions(possiblePos, oldPos)); blockedBoth = false; } if (blockedBoth) { var newPos = new BoardPos(oldPos); newPos.Diamonds.Add(diamondPos); positions.Add(newPos); } } return(positions); }
public BoardPos(BoardPos other) { Diamonds = new HashSet <Pos>(other.Diamonds); }
public static HashSet <BoardPos> GetNewPositions(KeyValuePair <BoardPos, double> oldPos) { BoardPos pos = new BoardPos(oldPos.Key); return(GetNewPositions(new Pos(0, pos.Diamonds.Where(x => x.X == 0).Max(x => x.Y) + 2), pos)); }