示例#1
0
        public void SpiralMatrixProblemTest()
        {
            var solution = new SpiralMatrixProblem();

            var matrix = new int[, ] {
                { 1, 2, 3 }, { 8, 9, 4 }, { 7, 6, 5 }
            };

            Helperrs.PrintMatrix(matrix);
            var result = solution.SpiralOrder(matrix);

            Assert.Equal(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, result);
            Helperrs.PrintArray(result);

            matrix = new int[, ] {
                { 1, 2, 3, 4 }, { 12, 13, 14, 5 }, { 11, 16, 15, 6 }, { 10, 9, 8, 7 }
            };
            Helperrs.PrintMatrix(matrix);
            result = solution.SpiralOrder(matrix);
            Helperrs.PrintArray(result);
            Assert.Equal(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }, result);

            matrix = new int[, ] {
                { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }
            };
            Helperrs.PrintMatrix(matrix);
            result = solution.SpiralOrder(matrix);
            Helperrs.PrintArray(result);
            Assert.Equal(new int[] { 1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7 }, result);
        }
示例#2
0
        public void GameOfLifeProblemTest()
        {
            var solution = new GameOfLifeProblem();

            var matrix = new int[][] { new int[] { 0, 1, 0 }, new int[] { 0, 0, 1 }, new int[] { 1, 1, 1 }, new int[] { 0, 0, 0 } };

            Helperrs.PrintMatrix(matrix);
            solution.GameOfLife(matrix);
            Helperrs.PrintMatrix(matrix);
        }
示例#3
0
        public void RotateImageProblemsTest()
        {
            var solution = new RotateImageProblems();

            var matrix = new int[, ] {
                { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 }
            };

            //var matrix = new int[,] { { 1, 2, 3 }, {4, 5, 6}, {7, 8, 9 } };

            Helperrs.PrintMatrix(matrix);
            solution.Rotate(matrix);
            Helperrs.PrintMatrix(matrix);
        }
示例#4
0
        public void Rotate(int[,] matrix)
        {
            var length = matrix.GetLength(0);

            if (length < 2)
            {
                return;
            }

            var i = 0;
            var j = 0;


            while (length > 1 && i < matrix.GetLength(0) / 2)
            {
                var nexti = j;
                var nextj = matrix.GetLength(0) - 1 - i;
                var index = 0;
                var buf   = new int[5];
                buf[0] = matrix[i, j];
                while (index < 4)
                {
                    buf[index + 1]       = matrix[nexti, nextj];
                    matrix[nexti, nextj] = buf[index];
                    var tmpindex = nexti;
                    nexti = nextj;
                    nextj = matrix.GetLength(0) - 1 - tmpindex;
                    index++;
                }
                Helperrs.PrintMatrix(matrix);

                if (j < length - 2)
                {
                    j++;
                }
                else
                {
                    i++; j = i; length--;
                }
            }
        }
示例#5
0
        public void SetMatrixZeroesProblemTest()
        {
            var solution = new SetMatrixZeroesProblem();

            var matrix = new int[, ] {
                { 1, 1, 1 }, { 1, 0, 1 }, { 1, 1, 1 }, { 1, 1, 1 }
            };

            Helperrs.PrintMatrix(matrix);
            solution.SetZeroes(matrix);
            Helperrs.PrintMatrix(matrix);

            var matrix2 = new int[, ] {
                { 0, 1, 1 }, { 1, 0, 1 }, { 1, 1, 1 }, { 1, 1, 1 }
            };

            Helperrs.PrintMatrix(matrix2);
            solution.SetZeroes(matrix2);
            Helperrs.PrintMatrix(matrix2);

            matrix2 = new int[, ] {
                { 1, 0 }
            };

            Helperrs.PrintMatrix(matrix2);
            solution.SetZeroes(matrix2);
            Helperrs.PrintMatrix(matrix2);

            matrix2 = new int[, ] {
                { 0 }, { 1 }
            };

            Helperrs.PrintMatrix(matrix2);
            solution.SetZeroes(matrix2);
            Helperrs.PrintMatrix(matrix2);

            matrix2 = new int[, ] {
                { 0 }
            };

            Helperrs.PrintMatrix(matrix2);
            solution.SetZeroes(matrix2);
            Helperrs.PrintMatrix(matrix2);


            matrix2 = new int[, ] {
                { 1 }
            };

            Helperrs.PrintMatrix(matrix2);
            solution.SetZeroes(matrix2);
            Helperrs.PrintMatrix(matrix2);

            matrix2 = new int[, ] {
                { 1, 1, 1 }, { 0, 1, 2 }
            };
            Helperrs.PrintMatrix(matrix2);
            solution.SetZeroes(matrix2);
            Helperrs.PrintMatrix(matrix2);

            matrix2 = new int[, ] {
                { 1, 0, 3 }
            };
            Helperrs.PrintMatrix(matrix2);
            solution.SetZeroes(matrix2);
            Helperrs.PrintMatrix(matrix2);


            matrix2 = new int[, ] {
                { -4, -2, 6, -7, 0 }, { -8, 6, -8, -6, 0 }, { 7, 2, -9, -6, -1 }
            };
            Helperrs.PrintMatrix(matrix2);
            solution.SetZeroes(matrix2);
            Helperrs.PrintMatrix(matrix2);
        }