Exemplo n.º 1
0
        /// <summary>
        /// To check if the puzzle is minimal or not.
        /// </summary>
        /// <param name="this">(<see langword="this"/> parameter) The puzzle to check.</param>
        /// <returns>A <see cref="bool"/> value indicating that.</returns>
        public static bool IsMinimal(this IReadOnlyGrid @this)
        {
            int hintCount = 0;

            int[] array     = @this.ToArray();
            var   valueList = new Queue <(int, int)>();

            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (array[i * 9 + j] != 0)
                    {
                        hintCount++;
                        valueList.Enqueue((i, j));
                    }
                }
            }

            int[][] tempArrays = new int[hintCount][];
            for (int i = 0; i < hintCount; i++)
            {
                var(r, c)                = valueList.Dequeue();
                tempArrays[i]            = (int[])array.Clone();
                tempArrays[i][r * 9 + c] = 0;
            }

            var solver = new BitwiseSolver();

            return(tempArrays.All(gridValues => !solver.Solve(Grid.CreateInstance(gridValues)).HasSolved));
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public override AnalysisResult Solve(IReadOnlyGrid grid)
        {
            _grid = grid;

            int[] gridValues = grid.ToArray();
            int[]? result = null;
            int solutionsCount = 0;
            var stopwatch      = new Stopwatch();

            try
            {
                stopwatch.Start();
                BacktrackinglySolve(ref solutionsCount, ref result, gridValues, 0);
                stopwatch.Stop();

                return(new AnalysisResult(
                           puzzle: grid,
                           solverName: SolverName,
                           hasSolved: true,
                           solution: Grid.CreateInstance(result ?? throw new NoSolutionException(grid)),
                           elapsedTime: stopwatch.Elapsed,
                           solvingList: null,
                           additional: null,
                           stepGrids: null));
            }
            catch (Exception ex)
            {
                stopwatch.Stop();

                return(new AnalysisResult(
                           puzzle: grid,
                           solverName: SolverName,
                           hasSolved: false,
                           solution: null,
                           elapsedTime: stopwatch.Elapsed,
                           solvingList: null,
                           additional: ex.Message,
                           stepGrids: null));
            }
        }