Exemple #1
0
        public static void FindAdjacent(int origin, Helfer.Matrix <int> mat, int index, HashSet <int> pos)
        {
            if (mat.GetElementAtIndex(index) == 0)
            {
                return;
            }
            pos.Add(index);
            int col = mat.GetCol(index);
            int row = mat.GetRow(index);

            if (origin != 3 && col > 0)
            {
                FindAdjacent(1, mat, mat.EncodePos(row, col - 1), pos);
            }
            if (origin != 4 && row > 0)
            {
                FindAdjacent(2, mat, mat.EncodePos(row - 1, col), pos);
            }
            if (origin != 1 && col < mat.mat.GetLength(1) - 1)
            {
                FindAdjacent(3, mat, mat.EncodePos(row, col + 1), pos);
            }
            if (origin != 2 && row < mat.mat.GetLength(0) - 1)
            {
                FindAdjacent(4, mat, mat.EncodePos(row + 1, col), pos);
            }
        }
        public static int Print_All_Solutions(Helfer.Matrix <int> matrix)
        {
            Stack <int> last = new Stack <int>();
            int         i    = 0;

            while (Sudoku_Check.ValidateSudoku(matrix, last, -1) && i <= MAX_COUNT_BEFORE_EXIT)
            {
                i++;
            }
            return(i);
        }
Exemple #3
0
 private static bool Find_fitting_value(Helfer.Matrix <int> mat, int ind)
 {
     for (int i = Math.Max(0, Math.Abs(mat.GetElementAtIndex(ind)) + 1); i <= 9; i++)
     {
         mat.SetElementAtIndex(ind, -i);
         if (Check_Index(mat, ind))
         {
             return(true);
         }
     }
     mat.SetElementAtIndex(ind, 0);
     return(false);
 }
Exemple #4
0
 protected static bool ValidateSudoku(Helfer.Matrix <int> mat, Stack <int> last = null, int debug = 1)
 {
     last = last ?? new Stack <int>();
     for (int i = last.Count > 0 ? last.Pop() : 0; i < mat.Length; i++)
     {
         //while (mat.GetElementAtIndex(i) > 0) if (++i >= mat.Length) return true;
         if (mat.GetElementAtIndex(i) > 0)
         {
             if (Check_Index(mat, i))
             {
                 continue;
             }
             else if (last.Count == 0)
             {
                 return(false);
             }
             else
             {
                 i = last.Pop() - 1;  //-1 Compensates for i++
             }
         }
         else
         {
             if (Find_fitting_value(mat, i))
             {
                 last.Push(i);
             }
             else if (last.Count == 0)
             {
                 return(false);
             }
             else
             {
                 i = last.Pop() - 1; //-1 Compensates for i++
             }
             if (debug > 0)
             {
                 Console.WriteLine("index: " + i + "\n" + mat);
             }
             if (debug > 0)
             {
                 Console.WriteLine(Helfer.Arrayausgabe(last.ToArray()));
             }
         }
     }
     if (debug == 0)
     {
         Console.WriteLine(mat);
     }
     return(true);
 }
        /*
         * Der Algorithums start beim Endpunkt der Matrix m,n => m-1m n-1
         * Vom Endpunkt aus werden alle Felder beginnend von rechts nach links und dabei zeilenweise nach oben passiert.
         * Bei auftreffen jedes Feldes wird der untere und rechte Wert betrachte und der jeweils Gößere auf das jetzige Feld aufaddiert.
         * Dies simuliert das Wählen des besten Pfades. Sind alle Werte aufaddiert befindet man sich auf dem Feld n=0,m=0.
         * Die endgültige Summe dieses Feldes entählt eine Akkumulation aller Münzen des optimalen Pfades.
         *
         *
         *
         *
         */

        public static void BacktrackSolver(Helfer.Matrix <int> matObj, InOut.Ergebnis erg)
        {
            int[,] mat = matObj.GetCopy().mat;
            for (int row = matObj.RowNum - 1; row >= 0; row--)
            {
                int right = 0;
                for (int col = matObj.ColNum - 1; col >= 0; col--)
                {
                    int down = row == matObj.RowNum - 1 ? 0 : mat[row + 1, col];
                    mat[row, col] = mat[row, col] + Math.Max(down, right);
                    right         = mat[row, col];
                }
            }

            Console.WriteLine(matObj);
            erg.Setze(mat[0, 0], Complexity.LINEAR, Complexity.CONSTANT);
        }
Exemple #6
0
        public static int Check_For_Mutliple_Solutions(Helfer.Matrix <int> matrix)
        {
            Stack <int> last = new Stack <int>();

            if (!Sudoku_Check.ValidateSudoku(matrix, last, 0))
            {
                return(-1);                                               // Check if Valid
            }
            Console.WriteLine("");
            Console.WriteLine("----------------------------");
            Console.WriteLine("");
            if (Sudoku_Check.ValidateSudoku(matrix, last, 0))
            {
                return(1);                                              // double Solution
            }
            else
            {
                return(0);
            }
        }
Exemple #7
0
        //SOL
        public static void FindNumIslands(Helfer.Matrix <int> mat, InOut.Ergebnis erg)
        {
            HashSet <int> pos   = new HashSet <int>();
            int           count = 0;

            for (int i = 0; i < mat.Length; i++)
            {
                if (pos.Contains(i))
                {
                    continue;
                }
                if (mat.GetElementAtIndex(i) == 0)
                {
                    continue;
                }
                count++;
                FindAdjacent(0, mat, i, pos);
            }
            erg.Setze(count, Complexity.LINEAR, Complexity.LINEAR);
        }
Exemple #8
0
        private static bool Check_Index(Helfer.Matrix <int> mat, int ind)
        {
            int num = Math.Abs(mat.GetElementAtIndex(ind));
            //Horizontal Check
            int row = ind / mat.RowNum;
            int col = ind % mat.ColNum;

            for (int i = 0; i < 9; i++)
            {
                if (Math.Abs(mat.GetElementAtIndex(i + row * mat.ColNum)) == num && i + row * mat.ColNum != ind)
                {
                    return(false);
                }
            }
            for (int i = 0; i < 9; i++)
            {
                if (Math.Abs(mat.GetElementAtIndex(col + i * mat.ColNum)) == num && col + i * mat.ColNum != ind)
                {
                    return(false);
                }
            }

            if (col >= 6)
            {
                col = 6;
            }
            else if (col >= 3)
            {
                col = 3;
            }
            else
            {
                col = 0;
            }
            if (row >= 6)
            {
                row = 6;
            }
            else if (row >= 3)
            {
                row = 3;
            }
            else
            {
                row = 0;
            }
            for (int i = 0; i < 9; i++)
            {
                int rel_ind = i + col + row * mat.ColNum;
                if (i > 5)
                {
                    rel_ind += 12;
                }
                else if (i > 2)
                {
                    rel_ind += 6;
                }
                if (Math.Abs(mat.GetElementAtIndex(rel_ind)) == num && rel_ind != ind)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #9
0
 public static void ValidateSudoku(Helfer.Matrix <int> mat, InOut.Ergebnis erg) => erg.Setze(ValidateSudoku(mat));
Exemple #10
0
 public static void Check_For_Mutliple_Solutions(Helfer.Matrix <int> matrix, InOut.Ergebnis erg) => erg.Setze(Check_For_Mutliple_Solutions(matrix));
 public static void Print_All_Solutions(Helfer.Matrix <int> matrix, InOut.Ergebnis erg) => erg.Setze(Print_All_Solutions(matrix));