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)); }
/* Encoding: posAbs = posY * T + posX where T > MaxValueOf(posX) => T == colCount * As long as T satisfies given Criteria following is true: * - posY = (int) posAbs / T * - posX = posA mod T * * // !!! T must be bigger than MaxValOf(posX) so pos X can be Encoded as the remainder of the number !!! * * posAbs = posY * (colCount+1) + posX * */ private static Pos BinarySearchMatrix(Helfer.Matrix <int> mat, int tar, ref int iterations) { int upBound = mat.Length - 1; int lowBound = 0; int current; while (upBound >= lowBound) { iterations++; current = (upBound + lowBound) / 2; int element = mat.GetElementAtIndex(current); if (element > tar) { upBound = current - 1; } else if (element < tar) { lowBound = current + 1; } else { return(new Pos(mat.GetCol(current) + 1, mat.GetRow(current) + 1)); //new Pos(current % mat.GetLength(0)+1, current / mat.GetLength(1)+1); //Element equals current; } } return(new Pos(-1, -1)); }
private static Pos BinarySearchMatrix2(Helfer.Matrix <int> mat, int tar, ref int iterations, int current = 0) { if (Helfer.BinarySearch(0, mat.Length, ref current, ref iterations, (ind) => mat.GetElementAtIndex(ind).CompareTo(tar))) { return(new Pos(mat.GetCol(current) + 1, mat.GetRow(current) + 1)); } else { return(new Pos(-1, -1)); } }
public static int 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 } return(mat.GetElementAtIndex(0)); }