コード例 #1
0
 public override bool Verify(int[] grid)
 {
     foreach (var c in PuzzleUtil.Orthogonal(Cell))
     {
         if (grid[c] >= grid[Cell])
         {
             return(false);
         }
     }
     return(true);
 }
コード例 #2
0
 public override bool Verify(int[] grid)
 {
     for (var cell = 0; cell < 81; cell++)
     {
         foreach (var c in PuzzleUtil.Orthogonal(cell, 9, 9))
         {
             if (Math.Abs(grid[c] - grid[cell]) == 1)
             {
                 return(false);
             }
         }
     }
     return(true);
 }
コード例 #3
0
        private static IEnumerable <int?[]> generateCombinations(bool isCol, int rowCol, int[] clue, IEnumerable <UniquenessConstraint> furtherRestrictions, int gridWidth, int gridHeight, int minValue, int maxValue)
        {
            var combinations        = new List <int?[]>();
            var affectedCenterCells = Enumerable.Range(0, isCol ? gridHeight : gridWidth).Select(ix => isCol ? rowCol + gridWidth * ix : ix + gridWidth * rowCol).ToArray();

            bool combinationAcceptable(int?[] combination)
            {
                if (furtherRestrictions == null)
                {
                    return(true);
                }
                foreach (var uniq in furtherRestrictions)
                {
                    for (var i = 0; i < uniq.AffectedCells.Length; i++)
                    {
                        if (combination[uniq.AffectedCells[i]] != null)
                        {
                            for (var j = i + 1; j < uniq.AffectedCells.Length; j++)
                            {
                                if (combination[uniq.AffectedCells[j]] == combination[uniq.AffectedCells[i]])
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                }
                return(true);
            }

            switch (clue.Length)
            {
            case 0:
                return(Enumerable.Empty <int?[]>());

            case 1:
            {
                for (var i1 = 0; i1 < affectedCenterCells.Length; i1++)
                {
                    var comb   = new int?[gridWidth * gridHeight];
                    var neigh1 = PuzzleUtil.Orthogonal(affectedCenterCells[i1], gridWidth, gridHeight).ToArray();
                    foreach (var numbers in PuzzleUtil.Combinations(minValue, maxValue, neigh1.Length, allowDuplicates: true).Where(c => c.Sum() == clue[0]))
                    {
                        for (var i = 0; i < neigh1.Length; i++)
                        {
                            comb[neigh1[i]] = numbers[i];
                        }
                        if (combinationAcceptable(comb))
                        {
                            combinations.Add((int?[])comb.Clone());
                        }
                    }
                }
                break;
            }

            case 2:
            {
                for (var i1 = 0; i1 < affectedCenterCells.Length; i1++)
                {
                    var comb   = new int?[gridWidth * gridHeight];
                    var neigh1 = PuzzleUtil.Orthogonal(affectedCenterCells[i1], gridWidth, gridHeight).ToArray();
                    foreach (var numbers in PuzzleUtil.Combinations(minValue, maxValue, neigh1.Length, allowDuplicates: true).Where(c => c.Sum() == clue[0]))
                    {
                        for (var i = 0; i < neigh1.Length; i++)
                        {
                            comb[neigh1[i]] = numbers[i];
                        }
                        if (!combinationAcceptable(comb))
                        {
                            continue;
                        }

                        for (var i2 = i1 + 1; i2 < affectedCenterCells.Length; i2++)
                        {
                            var neigh2 = PuzzleUtil.Orthogonal(affectedCenterCells[i2], gridWidth, gridHeight).ToArray();
                            foreach (var numbers2 in PuzzleUtil.Combinations(minValue, maxValue, neigh2.Length, allowDuplicates: true).Where(c => c.Sum() == clue[1]))
                            {
                                var comb2 = (int?[])comb.Clone();
                                for (var i = 0; i < neigh2.Length; i++)
                                {
                                    if (comb2[neigh2[i]] != null && comb2[neigh2[i]] != numbers2[i])
                                    {
                                        goto busted2;
                                    }
                                    comb2[neigh2[i]] = numbers2[i];
                                }
                                if (combinationAcceptable(comb2))
                                {
                                    combinations.Add(comb2);
                                }
                            }
                            busted2 :;
                        }
                    }
                }
                break;
            }

            default:
                throw new NotImplementedException("NeighborSumConstraint currently only supports up to 2 numbers per clue.");
            }

            return(combinations);
        }
コード例 #4
0
 public static IList <SvgConstraint> Generate(int[] sudoku) => Enumerable.Range(0, 81)
 .Where(cell => PuzzleUtil.Orthogonal(cell).All(c => sudoku[cell] > sudoku[c]))
 .Select(cell => new MaximumCell(cell))
 .ToArray();
コード例 #5
0
        }                            // for Classify

        protected override IEnumerable <Constraint> getConstraints() => PuzzleUtil.Orthogonal(Cell).Select(adj => new LessThanConstraint(new[] { adj, Cell }));