Ejemplo n.º 1
0
        /// <summary>
        /// Fills the matrix with consecutive integer values
        /// that correspond to the route of traversal.
        /// </summary>
        public void Traverse()
        {
            this.Clear();

            int counter = 1;
            Position position = new Position(0, 0);
            Location delta = new Location(Direction.Southeast);

            while (true)
            {
                this.matrix[position.Row, position.Col] = counter;

                if (!this.CanContinue(position))
                {
                    bool newPositionFound = this.TryFindNewPosition(out position);
                    if (newPositionFound)
                    {
                        counter++;
                        this.matrix[position.Row, position.Col] = counter;
                        delta.Direction = Direction.Southeast;
                    }
                    else
                    {
                        break;
                    }
                }

                while (!this.CanGoToPosition(position.Row + delta.Row, position.Col + delta.Col))
                {
                    delta.UpdateDirectionClockwise();
                }

                position.Update(delta);
                counter++;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Checks if there is a position which has remained unvisited
        /// during matrix traversal.
        /// </summary>
        /// <param name="newPosition">Keeps the first free position which has been found.</param>
        /// <returns>True if such a position has been found, otherwise - false.</returns>
        private bool TryFindNewPosition(out Position newPosition)
        {
            newPosition = new Position(0, 0);

            for (int row = 0; row < this.matrix.GetLength(0); row++)
            {
                for (int col = 0; col < this.matrix.GetLength(1); col++)
                {
                    if (this.matrix[row, col] == 0)
                    {
                        newPosition.Row = row;
                        newPosition.Col = col;
                        return true;
                    }
                }
            }

            return false;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Checks if the traversal can continue in any direction
        /// from the specified position.
        /// </summary>
        /// <param name="position">The current position.</param>
        /// <returns>True if another move is possible from the current position, otherwise - false.</returns>
        private bool CanContinue(Position position)
        {
            for (int i = 0; i < deltas.Length; i++)
            {
                if (this.CanGoToPosition(position.Row + deltas[i].Row, position.Col + deltas[i].Col))
                {
                    return true;
                }
            }

            return false;
        }