Ejemplo n.º 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);
            }
        }
Ejemplo n.º 2
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);
 }
Ejemplo n.º 3
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);
 }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }