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));
        }
        //SOL

        private static void SetZero_ConstantSpace(Helfer.Matrix <int> matrix, InOut.Ergebnis erg)
        {
            int[,] mat = matrix.mat;
            for (int i = 0; i < mat.GetLength(1); i++)
            {
                for (int j = 0; j < mat.GetLength(0); j++)
                {
                    if (mat[j, i] != 0)
                    {
                        continue;
                    }
                    mat[j, 0] = 0;
                    mat[0, i] = 0;
                }
            }
            // Parse first row and skip first col
            for (int i = 1; i < mat.GetLength(0); i++)
            {
                if (mat[i, 0] == 0)
                {
                    SetColZero(mat, i);
                }
            }
            // Parse first col
            for (int i = 0; i < mat.GetLength(1); i++)
            {
                if (mat[0, i] == 0)
                {
                    SetRowZero(mat, i);
                }
            }
            erg.Setze(matrix);
        }
        /*  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));
     }
 }
Exemplo n.º 5
0
        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));
        }
Exemplo n.º 6
0
 public static void TraverseMatrixFromBack_WithoutExtraMethods(Helfer.Matrix <int> mat, InOut.Ergebnis erg) => erg.Setze(TraverseMatrixFromBack_WithoutExtraMethods(mat.mat), Complexity.LINEAR, Complexity.CONSTANT);
Exemplo n.º 7
0
        //SOL
        private static Point[] SolveMaze(Helfer.Matrix <char> mat)
        {
            Point current = new Point(0, 0), prev = new Point(0, 0);

            Stack <Point>   path    = new Stack <Point>();
            HashSet <Point> visited = new HashSet <Point>();

            do
            {
                IList <Point> next = GetNeighbours(mat.mat, current, prev);
                if (next.Count == 1)
                {
                    if (next[0].X != prev.X + 2 && next[0].Y != prev.Y + 2 && next[0].X != prev.X - 2 && next[0].Y != prev.Y - 2)
                    {
                        visited.Add(prev);
                        path.Push(current);
                        visited.Add(next[0]);
                    }
                    prev    = current;
                    current = next[0];
                    continue;
                }
                else if (next.Count > 1)
                {
                    path.Push(current);
                }

                if (!visited.Contains(prev))
                {
                    visited.Add(prev);
                }
                prev    = current;
                current = null;
                foreach (Point p in next)
                {
                    if (visited.Contains(p))
                    {
                        continue;
                    }
                    current = p;
                    break;
                }

                if (current == null)
                {
                    if (path.Count > 0)
                    {
                        current = path.Pop();
                    }
                    else
                    {
                        throw new Exception("No Path Possible");
                    }
                }
                else
                {
                    visited.Add(current);
                }
            } while (path.Peek().X != mat.mat.GetLength(1) - 1 || path.Peek().Y != mat.mat.GetLength(0) - 1);
            Point[] arr = new Point[path.Count];
            for (int i = arr.Length - 1; i >= 0; i--)
            {
                arr[i] = path.Pop();
            }
            return(arr);
        }
Exemplo n.º 8
0
 private static void SolveMaze(Helfer.Matrix <char> mat, InOut.Ergebnis erg) => erg.Setze(SolveMaze(mat));
 public Input(string s, int target)
 {
     this.target = target;
     this.mat    = Helfer.Matrix <int> .GetIntMatrix(s);
 }