private bool AdvanceDestruction(char[,] board) { var destroyMe = new List <Tuple <int, int, int, int> >(); // rowStart, colStart, length, directionIndex for (var directionIndex = 0; directionIndex < lineSearchDirections.Length; directionIndex++) { var dir = lineSearchDirections[directionIndex]; for (var i = 0; i < board.GetLength(0); i++) { for (var j = 0; j < board.GetLength(1); j++) { var item = board[i, j]; if (item == '.') { continue; } var sameCnt = 1; while (true) { var r = i + sameCnt * dir[0]; var c = j + sameCnt * dir[1]; if (!board.AreIndicesAllowed(r, c) || board[r, c] != item) { break; } sameCnt++; } if (sameCnt >= 3) { destroyMe.Add(Tuple.Create(i, j, sameCnt, directionIndex)); } } } } foreach (var line in destroyMe) { var rowStart = line.Item1; var colStart = line.Item2; var length = line.Item3; var directionIndex = line.Item4; var dir = lineSearchDirections[directionIndex]; for (var i = 0; i < length; i++) { board[rowStart + i * dir[0], colStart + i * dir[1]] = '.'; } } return(destroyMe.Count > 0); }
private PointStruct <int> Go(char[,] maze, PointStruct <int> from, int direction) { var dirArray = directions[direction]; var to = new PointStruct <int>(from.X + dirArray[0], from.Y + dirArray[1]); if (!maze.AreIndicesAllowed(to.Y, to.X)) { throw new InvalidOperationException(string.Format("Not allowed to go to {0}", to)); } return(to); }
private void Solve(string pattern, char[,] board) { // Time O(boardheight * boardwidth * patterlen * countofsolutions) board.Print(); var dp = new DpArray(board); for (var pathLen = 0; pathLen < pattern.Length; pathLen++) { var searchme = pattern[pathLen]; for (var i = 0; i < board.GetLength(0); i++) { for (var j = 0; j < board.GetLength(1); j++) { if (board[i, j] != searchme) { continue; } if (pathLen == 0) { dp.AddFirstCandidate(i, j); } else { foreach (var dir in directions) { var ii = i + dir[0]; var jj = j + dir[1]; if (board.AreIndicesAllowed(ii, jj) && board[ii, jj] == pattern[pathLen - 1]) { dp.AddCandidate(i, j, ii, jj, pathLen); } } } } } dp.Print(); } foreach (var solution in dp.GetSolutions(pattern.Length)) { Console.WriteLine(Environment.NewLine.Join(solution.Path.Select(p => new { row = p.Y, col = p.X, ch = board[p.X, p.Y] }))); Console.WriteLine(); } }
private IEnumerable <char[, ]> GetNextTurns(char[,] board) { for (var i = 0; i < board.GetLength(0); i++) { for (var j = 0; j < board.GetLength(1); j++) { if (board[i, j] != '.') { foreach (var direction in turnSearchdirections) { var nexti = i + direction[0]; var nextj = j + direction[1]; if (board.AreIndicesAllowed(nexti, nextj) && board[i, j] != board[nexti, nextj]) { yield return(AdvanceBoard(board, i, j, nexti, nextj)); } } } } } }