Example #1
0
        private static bool CheckForNextMove(int[,] matrix, int currentRow, int currentCol)
        {
            var directions = new[]
            {
                "DownRight",
                "Down",
                "DownLeft",
                "Left",
                "UpLeft",
                "Up",
                "UpRight",
                "Right"
            };

            for (int i = 0; i < directions.Length; i++)
            {
                var currentDirection = directions[i];
                var deltas = DeltaUtils.GetDeltasByDirection(currentDirection);
                var nextRow = currentRow + deltas.X;
                var nextCol = currentCol + deltas.Y;
                if (CheckIfNextMoveIsValid(matrix, nextRow, nextCol))
                {
                    return true;
                }
            }

            return false;
        }
Example #2
0
        /// <summary>
        /// A rotating walk in the matrix is walk that starts form the top left corner of the matrix and goes in down-right direction. When no continuation is available at the current direction , the direction is changed to the next possible clockwise.
        /// </summary>
        /// <param name="matrix">Matrix to fill.</param>
        internal static void FillRotatingWalkMatrix(int[,] matrix)
        {
            var rows = matrix.GetLength(0);
            var cols = matrix.GetLength(1);

            int row = Constants.StartRow;
            int col = Constants.StartCol;
            int cellValue = Constants.CellsStartValue;
            var direction = Constants.Directions[0];

            var deltas = DeltaUtils.GetDeltasByDirection(direction);
            bool hasFreeCells = true;

            while (hasFreeCells)
            {
                matrix[row, col] = cellValue;

                if (!CheckForNextMove(matrix, row, col))
                {
                    int[] nextFreeCell = FindUnvisitedCell(matrix);
                    if ((nextFreeCell[0] > -1) && (nextFreeCell[1] > -1))
                    {
                        row = nextFreeCell[0];
                        col = nextFreeCell[1];
                        cellValue++;
                        direction = Constants.Directions[0];
                        deltas = DeltaUtils.GetDeltasByDirection(direction);
                    }
                    else
                    {
                        // All cells in matrix are filled.
                        hasFreeCells = false;
                    }
                }
                else
                {
                    var nextRow = row + deltas.X;
                    var nextCol = col + deltas.Y;
                    if (!CheckIfNextMoveIsValid(matrix, nextRow, nextCol))
                    {
                        while (!CheckIfNextMoveIsValid(matrix, nextRow, nextCol))
                        {
                            direction = ChangeDirection(direction);
                            deltas = DeltaUtils.GetDeltasByDirection(direction);
                            nextRow = row + deltas.X;
                            nextCol = col + deltas.Y;
                        }
                    }

                    row += deltas.X;
                    col += deltas.Y;
                    cellValue++;
                }
            }
        }