Solve Sudoku Puzzle[C#]

Scattered123

Well-known member
  • Nov 18, 2012
    949
    323
    63
    Does it matter?
    දැන් ටික කාලෙකට මගේ පරණ යාලුවෙක් ආව කැම්පස් එකේ Computer Science කරන එකෙක්. උන්ට ඇගයීමක් දීලා තිබ්බා සුදොකු පස්ල් එකක් විසදන්න. මේ මම ඒකට ලියපු කෝඩ් එකක්, C# වලින් තියෙන්නෙ. කිරියෙ කාට හරි උවමනාවක් වෙයි කියල හිතුන.

    අලුතෙන් ප්‍රෝග්‍රැමින් කරන උන්ට වැදගත් වෙයි. :rolleyes:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
    
            static int roww = 0;
            static int coll = 0;
    
            static int[,] puzzle = new int[,]
            {
                {6, 0, 0,   0, 7, 8,   0, 0, 0},
                {0, 0, 4,   6, 0, 0,   0, 8, 0},
                {3, 0, 5,   0, 0, 9,   7, 0, 0},
                
                {5, 0, 3,   8, 0, 7,   0, 0, 0},
                {8, 0, 9,   4, 0, 6,   3, 0, 5},
                {0, 0, 0,   9, 0, 3,   8, 0, 7},
    
                {0, 0, 8,   3, 0, 0,   6, 0, 2},
                {0, 2, 0,   0, 0, 1,   4, 0, 0},
                {0, 0, 0,   7, 8, 0,   0, 0, 1}
            };
    
            static void Main(string[] args)
            {
                Solver();
                Console.Read();
            }
    
            static void Solver()
            {
                int count = 0;
                for (int q = 0; q < 9; q++)
                {
                    roww = q;
                    for (int w = 0; w < 9; w++)
                    {
                        coll = w;
                        if (puzzle[q, w] == 0)
                        {
                            count++;
                            Solve();
                        }
                    }
                }
                if (count != 0)
                {
                    Solver();
                }
                else
                {
                    for (int i = 0; i < 9; i++)
                    {
                        for (int w = 0; w < 9; w++)
                        {
                            Console.Write(puzzle[i, w] + " ");
                        }
                        Console.WriteLine("");
                    }
                }
            }
    
            static void Solve()
            {
    
                List<int> ios = new List<int>();
                List<int> ios2 = new List<int>();
    
                for (int i = 0; i < 9; i++)
                {
                    if (puzzle[roww, i] != 0)
                    {
                        ios.Add(puzzle[roww, i]);
                        //Console.WriteLine(puzzle[roww, i]);
    
                    }
    
                }
                //Console.WriteLine("\n");
                for (int i = 0; i < 9; i++)
                {
                    if (puzzle[i, coll] != 0)
                    {
                        ios.Add(puzzle[i, coll]);
                        //Console.WriteLine(puzzle[i, coll]);
                    }
                }
                //Console.WriteLine("\n");
    
    
                for (int a = 1; a <= 9; a++)
                {
                    if (!ios.Contains(a))
                    {
                        ios2.Add(a);
    
                        //Console.WriteLine(a);
                    }
                }
                //Console.WriteLine("\n");
                int ia = 0;
                int b = 0;
                List<int> ioss = ios2;
                int[] onto = ios2.ToArray();
    
                foreach (int val in ios2)
                {
                    if (CheckRegion(val) != 1)
                    {
                        ia++;
                    }
                }
    
                if (ia == 1)
                {
                    for (int i = 0; i < ios2.Count; i++)
                    {
                        if (CheckRegion(ios2[i]) != 1)
                        {
                            b = i;
                        }
                    }
                }
    
                if (ia == 1)
                {
                    //Console.WriteLine(roww + "-" + coll + "-" + ios2[b]);
                    puzzle[roww, coll] = ios2[b];
                    //Solve();
                }
                else
                {
                    
                    //Solve();
                }
    
                //Console.WriteLine(ia);
            }
    
            static int CheckRegion(int value)
            {
                int rows = 0;
                int cols = 0;
    
                if (roww >= 0 && roww < 3)
                {
                    rows = 3;
                }
                if (roww >= 3 && roww < 6)
                {
                    rows = 6;
                }
                if (roww >= 6 && roww < 9)
                {
                    rows = 9;
                }
    
                if (coll >= 0 && coll < 3)
                {
                    cols = 3;
                }
                if (coll >= 3 && coll < 6)
                {
                    cols = 6;
                }
                if (coll >= 6 && coll < 9)
                {
                    cols = 9;
                }
    
    
                for (int i = rows - 3; i < 3 + (rows - 3); i++)
                {
                    for (int j = cols - 3; j < 3 + (cols - 3); j++)
                    {
                        if (puzzle[i, j] == value)
                        {
                            return 1;
                        }
                    }
                }
                return 0;
            }
    
            static int CheckIfZero()
            {
                return 0;
            }
        }
    }
     

    Scattered123

    Well-known member
  • Nov 18, 2012
    949
    323
    63
    Does it matter?
    kohomada add karanne. sorry.

    Code:
                {6, 0, 0,   0, 7, 8,   0, 0, 0},
                {0, 0, 4,   6, 0, 0,   0, 8, 0},
                {3, 0, 5,   0, 0, 9,   7, 0, 0},
                
                {5, 0, 3,   8, 0, 7,   0, 0, 0},
                {8, 0, 9,   4, 0, 6,   3, 0, 5},
                {0, 0, 0,   9, 0, 3,   8, 0, 7},
    
                {0, 0, 8,   3, 0, 0,   6, 0, 2},
                {0, 2, 0,   0, 0, 1,   4, 0, 0},
                {0, 0, 0,   7, 8, 0,   0, 0, 1}

    code eke tiyenne bn onna oka. uba danne nathi ewa 0, uba danna ewa purola, danne nati ewwa 0 tiyala oka run kalama, solution ekak tiyenm labenwa full puzzle eka:yes:
     
    Last edited: