private int CalcDensity(Board board, Point[] points) { var ret = 0; for (int i = 0; i < points.Length; i++) { var point = points[i]; for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { int ni = point.Row + dx, nj = point.Col + dy; if (0 <= ni && ni < board.Height && 0 <= nj && nj < board.Width) { if (board.Field[ni][nj] == CellState.Free) continue; if (IsNeighbors(point, nj, ni)) { ret++; } } } } } return ret; }
public string Generate(Board _board, GameUnit _unit, GameUnit _finishUnit) { var stringBuilder = new StringBuilder(); var words = MagicWordsStore.Words .Concat(SpecialWords) .ToArray(); var usedUnits = new HashSet<GameUnit>(); while (!_unit.Equals(_finishUnit)) { if (TimeLimiter.NeedStop()) break; foreach (var powerWord in words.OrderByDescending(x => x.Length)) { if (TimeLimiter.NeedStop()) break; var newlyUsedUnits = new HashSet<GameUnit>(); var currentUnit = _unit; var fail = false; for (var i = 0; i < powerWord.Length; ++i) { var command = powerWord[i]; newlyUsedUnits.Add(currentUnit); var nextUnit = currentUnit.MakeStep(CommandConverter.Convert(command)); var locked = !_board.IsValid(nextUnit); if (newlyUsedUnits.Contains(nextUnit) || usedUnits.Contains(nextUnit) || (locked && i < powerWord.Length - 1) || (locked && !nextUnit.Equals(_finishUnit))) { fail = true; break; } if (!locked) { currentUnit = nextUnit; } } var allUsedUnits = new HashSet<GameUnit>(usedUnits.Union(newlyUsedUnits)); if (!fail && ReachableStatesGetter.CanReach(_board, currentUnit, false, allUsedUnits, _finishUnit)) { _unit = currentUnit; usedUnits = allUsedUnits; stringBuilder.Append(powerWord); break; } } } foreach (var command in Enum.GetValues(typeof(Command)).Cast<Command>().Except(new[] { Command.Empty })) { if (!_board.IsValid(_unit.MakeStep(command))) { stringBuilder.Append(CommandConverter.CovertToAnyChar(command)); break; } } return stringBuilder.ToString(); }
public static GameUnit[] Get(Board board, GameUnit unit, bool onlyLocked, HashSet<GameUnit> usedUnits = null) { var newlyUsedUnits = new HashSet<GameUnit>(); var lockedUnits = onlyLocked ? new HashSet<GameUnit>() : null; Dfs(board, unit, newlyUsedUnits, usedUnits ?? new HashSet<GameUnit>(), lockedUnits); return onlyLocked ? lockedUnits.ToArray() : newlyUsedUnits.ToArray(); }
public Game(Board board, GameUnit current, Unit[] unitsSequence, int currentUnitNumber, int lastUnitLinesCleared, int score, int problemId, long seed, string lastSymbols, int wordsMask) : this() { Board = board; Current = current; UnitsSequence = unitsSequence; CurrentUnitNumber = currentUnitNumber; LastUnitLinesCleared = lastUnitLinesCleared; Score = score; ProblemId = problemId; Seed = seed; LastSymbols = lastSymbols; WordsMask = wordsMask; }
private static void Dfs(Board board, GameUnit unit, HashSet<GameUnit> newlyUsedUnits, HashSet<GameUnit> previouslyUsedUnits, HashSet<GameUnit> lockedUnits) { newlyUsedUnits.Add(unit); foreach (var command in Commands) { var nextUnit = unit.MakeStep(command); if (!previouslyUsedUnits.Contains(nextUnit) && !newlyUsedUnits.Contains(nextUnit)) { if (board.IsValid(nextUnit)) { Dfs(board, nextUnit, newlyUsedUnits, previouslyUsedUnits, lockedUnits); } else if (lockedUnits != null) { lockedUnits.Add(unit); } } } }
private static bool Dfs2(Board board, GameUnit unit, HashSet<GameUnit> newlyUsedUnits, HashSet<GameUnit> previouslyUsedUnits, HashSet<GameUnit> lockedUnits, GameUnit finishUnit) { if (unit.Equals(finishUnit)) return true; newlyUsedUnits.Add(unit); foreach (var command in Commands) { var nextUnit = unit.MakeStep(command); if (!previouslyUsedUnits.Contains(nextUnit) && !newlyUsedUnits.Contains(nextUnit)) { if (board.IsValid(nextUnit) && Dfs2(board, nextUnit, newlyUsedUnits, previouslyUsedUnits, lockedUnits, finishUnit)) { return true; } else if (lockedUnits != null) { lockedUnits.Add(unit); } } } return false; }
public static bool CanReach(Board board, GameUnit unit, bool onlyLocked, HashSet<GameUnit> usedUnits, GameUnit finishUnit) { var newlyUsedUnits = new HashSet<GameUnit>(); var lockedUnits = onlyLocked ? new HashSet<GameUnit>() : null; return Dfs2(board, unit, newlyUsedUnits, usedUnits ?? new HashSet<GameUnit>(), lockedUnits, finishUnit); }
public string Generate(Board _board, GameUnit _unit, GameUnit _finishUnit) { board = _board; target = _finishUnit; moves = new Dictionary<Tuple<GameUnit, string>, GameUnit>(); words = MagicWordsStore.Words.Concat(SpecialWords).OrderByDescending(x => x.Length).ToArray(); dp = new Dictionary<State, DpInfo>(); var state = new State(_unit, Command.Empty, false, true, null); CalcDp(state); var stringBuilder = new StringBuilder(); while (!(state.Unit.Equals(target) && state.IsLocked)) { var dpInfo = dp[state]; stringBuilder.Append(dpInfo.Word); state = dpInfo.NextState.Value; } return stringBuilder.ToString(); }