Exemplo n.º 1
0
        static void Main()
        {
            Helper.SetupConsole();

            Helper.PrintColorText("Sequence in Matrix\n\n", "cyan");

            int[] dimensions = ReadDimensions("matrix");

            string[,] myMatrix = new string[dimensions[0], dimensions[1]];
            //{
            //    { "ha", "fifi", "ho", "hi" },
            //    { "fo", "ha", "hi", "xx" },
            //    { "xxx", "ho", "ha", "xx" },
            //};

            ReadMatrix(myMatrix);

            MatrixPattern <string> longestPattern = GetLongestPattern(myMatrix);

            PrintMatrix(myMatrix, longestPattern.PatternMask, paddingSize + 1);
            Console.WriteLine();

            Console.Write("Element: ");
            Helper.PrintColorText(string.Format("'{0}'\n", longestPattern.Footprint), "cyan");
            Console.Write("Sequence Length: ");
            Helper.PrintColorText(string.Format("{0}\n", longestPattern.Length), "cyan");

            Helper.Restart(Main);
        }
Exemplo n.º 2
0
        static MatrixPattern <T> GetLongestPattern <T>(T[,] matrix)
            where T : IConvertible
        {
            MatrixPattern <T> result = null;

            List <Func <int, int, T, T[, ], bool[, ], int> > sequenceChecks =
                new List <Func <int, int, T, T[, ], bool[, ], int> >
            {
                patternChecks.CheckHorizontalSequence,
                patternChecks.CheckVerticalSequence,
                patternChecks.CheckRightDiagonalSequence,
                patternChecks.CheckLeftDiagonalSequence
            };

            int maxSequenceLength = 0;

            int n = matrix.GetLength(0),
                k = matrix.GetLength(1);

            bool[,] mask = new bool[n, k];

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < k; j++)
                {
                    var currentPattern = new MatrixPattern <T>(matrix[i, j], i, j);

                    int lenghtAsString = matrix[i, j].ToString().Length;

                    if (lenghtAsString > paddingSize)
                    {
                        paddingSize = lenghtAsString;
                    }

                    int currentSequenceLength = CheckSequences(matrix, currentPattern, mask, sequenceChecks);
                    if (currentSequenceLength > maxSequenceLength)
                    {
                        maxSequenceLength = currentSequenceLength;

                        result = currentPattern;
                    }
                }
            }

            return(result);
        }
Exemplo n.º 3
0
        private static int CheckSequences <T>(T[,] matrix, MatrixPattern <T> pattern, bool[,] mask
                                              , List <Func <int, int, T, T[, ], bool[, ], int> > callbacks)
            where T : IConvertible
        {
            T footprint = pattern.Footprint;

            int count = 0,
                top   = pattern.StartRow,
                left  = pattern.StartCol;

            foreach (var check in callbacks)
            {
                Array.Clear(mask, 0, mask.Length);
                count = check(top, left, footprint, matrix, mask);

                if (count > pattern.Length)
                {
                    pattern.Length      = count;
                    pattern.PatternMask = (bool[, ])mask.Clone();
                }
            }

            return(pattern.Length);
        }