예제 #1
0
        private void guessAll()
        {
            long guessStart = DateTime.Now.Ticks;

            for (int i = 0; i < size; ++i)
            {
                for (int j = 0; j < size; ++j)
                {
                    if (tracker.board.board[i][j] != '-')
                    {
                        continue;
                    }
                    CharSet possib = tracker.progress[i][j];
                    if (possib.size() == 2)                     // minimal
                    {
                        for (int l = 0; l < 2; ++l)
                        {
                            Tracker newTracker = new Tracker(tracker);
                            newTracker.board.setCell(i, j, possib.getList()[l]);
                            Solver  newSolver  = new Solver(newTracker);
                            Tracker retTracker = newSolver.run();
                            if (retTracker.valid)
                            {
                                singleSquareElapsed += newSolver.singleSquareElapsed;
                                singleRegionElapsed += newSolver.singleRegionElapsed;
                                guessCnt            += newSolver.guessCnt + 1;
                                ++tracker.solutionCnt;
                                if (tracker.solutionCnt == 2 || retTracker.solutionCnt == 2)
                                {
                                    tracker             = retTracker;
                                    tracker.solutionCnt = 2;
                                    i = size; j = size; l = 2;                                     // break from loop
                                }
                            }
                        }
                    }
                }
            }
            long guessEnd   = DateTime.Now.Ticks;
            long timeMicros = (guessEnd - guessStart) / 10;

            guessTimeElapsed += timeMicros;
        }
예제 #2
0
        static void Main(string[] args)
        {
            if (args.Length == 0 || args.Length == 1 && args[0] == "-h")
            {
                printHelp();
                return;
            }

            Board sudokuBoard;

            try
            {
                sudokuBoard = FileInterface.readFromFile(args[0]);
            }
            catch (Exception e)
            {
                if (File.Exists(args[0]))
                {
                    string[] allLines = File.ReadAllLines(args[0]);
                    foreach (string l in allLines)
                    {
                        Console.WriteLine(l);
                    }
                }
                Console.WriteLine();
                Console.WriteLine(e.Message);
                return;
            }

            Board initialBoard = new Board(sudokuBoard);

            Tracker tracker = new Tracker(sudokuBoard);
            Solver  solver  = new Solver(tracker);

            tracker = solver.run();

            if (!tracker.valid)
            {
                Console.WriteLine("ERROR: Invalid puzzle.");
                return;
            }

            if (tracker.solutionCnt > 1)
            {
                Console.WriteLine("WARNING: More than one solution found. One is shown.");
            }

            if (args.Length > 1)
            {
                using (var writer = new StreamWriter(args[1]))
                {
                    Console.SetOut(writer);
                    FileInterface.writeToFile(initialBoard, tracker.board, solver, solver.totalTimeElapsed);
                }
            }
            else
            {
                FileInterface.writeToFile(initialBoard, tracker.board, solver, solver.totalTimeElapsed);
            }
            Console.SetOut(new StreamWriter(Console.OpenStandardOutput()));
        }