コード例 #1
0
        static Form1 show;                //This is the GUI, don't try and understand its code. We won't be using one at CodeQuest

        static void Main(string[] args)
        {
            //Setup
            var raw       = File.ReadAllLines(file); //Reads the file
            int numTables = int.Parse(raw[0]);       //Gets the number on the first line that says how many tables there are

            tables = new List <SudokuTable>();       //Initializes tables list

            int rawIndex = 1;

            for (int i = 0; i < numTables; i++) //Once for each whatever
            {
                var table = new SudokuTable();
                for (int j = 0; j < 9; j++, rawIndex++)   //Go through next 9
                {
                    table.PopulateLine(j, raw[rawIndex]); //Writes the sudoku table to the object
                }
                tables.Add(table);
            }

            show = new Form1(); //GUI stuff
            show.Show();        //GUI stuff

            //Processing
            foreach (SudokuTable t in tables)
            {
                //This block of code finds the first unknown point. It takes into account the possibility that the first point, [0, 0], may or may not be known
                Point p = new Point(-1, 0);
                t.NextUnknown(ref p);

                show.UpdateTable(t, true);                 //GUI stuff

                int iterations = 0;                        //GUI stuff but not really, but it's only used by the GUI so
                while (!t.IsTableComplete())               //Continue until table is complete. This method is run every iteration, and could probably be optimized. But it's already almost instantaneous so I'm not going to bother
                {
                    t.table[p.x, p.y] = null;              //Sets the value at this point to null to prevent it from counting toward the checking for numbers. There's no checking here to make sure the point isn't already known because that's done already with the NextUnknown and PreviousUnknown methods

                    int?num = t.GenerateValidNumber(p);    //Gets (guesses, basically) the next potentially valid number. Counts up from 1 to 9. Simple stuff

                    if (num == null)                       //If the next valid number is null, it means that there are no valid numbers and the solution must be wrong somewhere.
                    {
                        t.knownExcluded[p.x, p.y].Clear(); //Removes any excluded values, as the assumption they can't be right is based off of an invalid solution
                        t.PreviousUnknown(ref p);          //Goes to previous position
                    }
                    else
                    {
                        t.table[p.x, p.y] = num;                 //Inserts number into table
                        t.knownExcluded[p.x, p.y].Add((int)num); //Excludes this because if it needs to go back later (which, trust me, it will) it saves a step
                        t.NextUnknown(ref p);                    //Goes to next number in table
                    }

                    //LOOK HERE IF YOU WANT THE GUI TO UPDATE MORE FREQUENTLY
                    //This entire block of code exists purely for graphical reasons, along with the iterations variable
                    if (iterations % 10000 == 0) //Set that number that's 10,000 right now to whatever you want. It refreshes the graph every (that many) iterations. Lower numbers make it painfully slow.
                    {
                        show.UpdateTable(t);
                    }
                    iterations++;
                }

                t.Output(); //Outputs to console

                //GUI stuff in this block
                show.BackColor = Color.LightGreen;
                show.UpdateTable(t);
                Thread.Sleep(50);
                show.BackColor = Color.White;
            }

            Console.ReadLine(); //This is here so the console doesn't close when the program terminates, and wouldn't be there in the competition. It just waits for user input, basically.
        }