Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var m = MatrixSpiralFilling.FillMatrix(6);

            MatrixSpiralFilling.PrintMatrix(m, -3);

            var idx = UnorderedSubstring.FindIndex("hggtyug7ghh7jf5lkj8kljk6fgg", "7ghf7jh");

            Console.WriteLine(idx);

            //TestDataSize();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Fills the matrix in using whole numbers strating from 1. Moves from the matrix center in spiral direction up-->left-->down-->right.
        /// </summary>
        /// <param name="N">Matrix size.</param>
        /// <returns>Filled in matrix.</returns>
        public static int[,] FillMatrix(int N)
        {
            if (N < 1)
            {
                throw new Exception($"Invalid matrix size {N}. Should be 1 or greater.");
            }
            if (N == 1)
            {
                return new int[1, 1] {
                           { 1 }
                }
            }
            ;

            var m = new int[N, N];

            var active       = true;         //Used for stopping. We are going to stop when we hit any non-zero cell
            var step         = 1;            //Number of steps in one direction. When step becomes zero we change the direction
            var dir          = Direction.Up; //Movement direction
            var currentCount = 1;            //A counter to keep current number to put into a cell

            int centerX = N / 2, centerY = N / 2;

            m[centerY, centerX] = 1; //Filling in the centeral cell. We do it manually to align the step, as it needs step = 2, but next movement needs step = 1

            for (int x = centerX, y = centerY - 1, sideCount = 1 /*passed edges number*/; active; ++sideCount)
            {
                var startDir = dir;

                for (int i = step; i > 0; --i)
                {
                    //Console.WriteLine($"{x} : {y}, {step}, {dir} = {currentCount + 1}, s={step}");
                    if (m[y, x] != 0)
                    {
                        active = false; //we heat a filled in cell, so we stop as there are no any cells left
                        break;
                    }
                    m[y, x] = ++currentCount;

                    MatrixSpiralFilling.MakeStep(ref x, ref y, dir);
                    if (!MatrixSpiralFilling.InBounds(N, x, y, dir)) //if we are out of the matrix we have to change the direction
                    {
                        MatrixSpiralFilling.ChangeDirection(ref x, ref y, ref dir);
                    }
                }

                //Each time, when we haven't reached the border, we need to change direction too
                if (startDir == dir)
                {
                    MatrixSpiralFilling.ChangeDirection(ref x, ref y, ref dir);
                }

                //We need to increase the movement size on each even number of passed edges
                if (sideCount % 2 == 0)
                {
                    step++;
                }
            }

            return(m);
        }