Exemplo n.º 1
0
        public static Output TraverseMatrixFromBack(Helfer.Matrix <int> mat)
        {
            for (int index = mat.Length - 2; index >= 0; index--)
            {
                int botEl   = mat.GetRow(index) == mat.RowNum - 1 ? int.MaxValue : mat.GetElement(mat.GetRow(index) + 1, mat.GetCol(index)); // If LAST ROW: int.Max ELSE: take bottom element
                int rightEl = mat.GetCol(index) == mat.ColNum - 1 ? int.MaxValue : mat.GetElement(mat.GetRow(index), mat.GetCol(index) + 1); // If LAST COL: int.Max ELSE: take right element
                mat.SetElementAtIndex(index, mat.GetElementAtIndex(index) + Math.Min(botEl, rightEl));                                       // Add smaller accumulated element to current
                                                                                                                                             // This means it goes along the path with the smaller sum
                                                                                                                                             // The smaller sum accumulates along the path
            }

            Helfer.Point[] trace = new Helfer.Point[mat.RowNum + mat.ColNum - 1];
            for (int ind = 0, ptTrace = 0; ptTrace < trace.Length;)
            {
                trace[ptTrace++] = new Helfer.Point(mat.GetRow(ind), mat.GetCol(ind));
                int botEl   = mat.GetRow(ind) == mat.RowNum - 1 ? int.MaxValue : mat.GetElement(mat.GetRow(ind) + 1, mat.GetCol(ind));
                int rightEl = mat.GetCol(ind) == mat.ColNum - 1 ? int.MaxValue : mat.GetElement(mat.GetRow(ind), mat.GetCol(ind) + 1);

                if (botEl < rightEl)
                {
                    ind = mat.EncodePos(mat.GetRow(ind) + 1, mat.GetCol(ind));
                }
                else
                {
                    ind = mat.EncodePos(mat.GetRow(ind), mat.GetCol(ind) + 1);
                }
            }

            return(new Output(mat.GetElementAtIndex(0), trace, mat.mat));
        }
Exemplo n.º 2
0
        public static Output TraverseMatrixFromBack_WithoutExtraMethods(int[,] mat)
        {
            for (int index = mat.Length - 2; index >= 0; index--)
            {
                int col = index % mat.GetLength(1);
                int row = index / mat.GetLength(1);

                int botEl   = row == mat.GetLength(0) - 1 ? int.MaxValue : mat[row + 1, col]; // If LAST ROW: int.Max ELSE: take bottom element
                int rightEl = col == mat.GetLength(1) - 1 ? int.MaxValue : mat[row, col + 1]; // If LAST COL: int.Max ELSE: take right element
                mat[row, col] += Math.Min(botEl, rightEl);                                    // Add smaller accumulated element to current
                // This means it goes along the path with the smaller sum
                // The smaller sum accumulates along the path
            }

            Helfer.Point[] trace = new Helfer.Point[mat.GetLength(0) + mat.GetLength(1) - 1];
            for (int ind = 0, ptTrace = 0; ptTrace < trace.Length;)
            {
                int col = ind % mat.GetLength(1);
                int row = ind / mat.GetLength(1);

                trace[ptTrace++] = new Helfer.Point(row, col);

                int botEl   = row == mat.GetLength(0) - 1 ? int.MaxValue : mat[row + 1, col];
                int rightEl = col == mat.GetLength(1) - 1 ? int.MaxValue : mat[row, col + 1];

                if (botEl < rightEl)
                {
                    ind = (row + 1) * mat.GetLength(1) + col;
                }
                else
                {
                    ind = row * mat.GetLength(1) + col + 1;
                }
            }

            return(new Output(mat[0, 0], trace, mat));
        }