Esempio n. 1
0
        //Makes a new SSudoku object and runs the solve algoritm with local search 50 times for every sudoku for every random walk length, outputs the results to a text file
        private static void solve(int N, int i)
        {
            //Keep track of time with a stopwatch
            Stopwatch sw = new Stopwatch();
            SSudoku   s  = new SSudoku();

            //Initialize the class with the original board and size

            double time = 0;

            double bestTime = double.MaxValue;
            int    bestP    = 0;

            for (int r = 0; r < 10; r++)
            {
                readBoardFromFile(r);
                for (int p = 2; p < 40; p += 2)
                {
                    for (int k = 0; k < 50; k++)
                    {
                        //Actually solve the sudoku

                        s.init(OriginalSudoku, N);
                        sw.Reset();
                        sw.Start();
                        s.solve(i + 5 + k, p);
                        // Console.WriteLine("Solved");
                        sw.Stop();
                        time += sw.Elapsed.TotalSeconds;
                    }
                    streamwriter.WriteLine(time / 50);
                    streamwriter.Flush();
                    // Console.WriteLine(time / 10);
                    if (time / 50 < bestTime)
                    {
                        bestTime = time / 50;
                        bestP    = p;
                    }
                    time = 0;
                }
                streamwriter.WriteLine();
                Console.WriteLine("Solved " + (r + 1) + " with " + (bestP) + "!");
                bestTime = double.MaxValue;
                bestP    = 0;
            }
        }
Esempio n. 2
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Please input a Sudoku.");

            //Read in the board with spaces or without, whatever suits you
            int N = readBoard();

            //int N = readBoardFromFile(6);

            //Queue for the chart window
            oldscores = new Queue <int>(100);

            //Start a new thread for the chart window
            if (chartwindow)
            {
                System.Threading.Thread mythread;
                mythread = new System.Threading.Thread(new System.Threading.ThreadStart(OpenWindow));
                mythread.Start();
            }


            //Solve the sudoku
            SSudoku sudoku = new SSudoku();

            sudoku.init(OriginalSudoku, N);
            sudoku.solve(1, 8);
            sudoku.print();

            //For multithreading

            /*
             * System.Threading.Thread t = new System.Threading.Thread(() => solve(N, 2));
             * t.Start();
             *
             * //While the sudoku is searching keep the main thread sleeping
             * while (t.IsAlive)
             * {
             *  Thread.Sleep(1000);
             * }
             *
             * t.Join();
             */


            Console.ReadLine();
        }