Exemplo n.º 1
0
    private bool isUnique(int[] rowSums, int[] rowVoltorbs, int[] colSums, int[] colVoltorbs, int[] givenCells)
    {
        var puzzle = new Puzzle(25, 0, 3);

        for (var i = 0; i < 5; i++)
        {
            puzzle.AddConstraint(new CombinationsConstraint(Enumerable.Range(0, 5).Select(j => j + 5 * i), PuzzleUtil.Combinations(0, 3, 5, true).Where(c => c.Sum() == rowSums[i] && c.Count(v => v == 0) == rowVoltorbs[i])));
            puzzle.AddConstraint(new CombinationsConstraint(Enumerable.Range(0, 5).Select(j => i + 5 * j), PuzzleUtil.Combinations(0, 3, 5, true).Where(c => c.Sum() == colSums[i] && c.Count(v => v == 0) == colVoltorbs[i])));
        }
        for (var gcIx = 0; gcIx < givenCells.Length; gcIx++)
        {
            puzzle.AddConstraint(new GivenConstraint(givenCells[gcIx], 0));
        }

        bool[] voltorbs = null;

        foreach (var solution in puzzle.Solve())
        {
            if (voltorbs == null)
            {
                voltorbs = solution.Select(v => v == 0).ToArray();
            }
            else
            {
                for (var i = 0; i < solution.Length; i++)
                {
                    if ((solution[i] == 0) != voltorbs[i])
                    {
                        return(false);
                    }
                }
            }
        }
        return(true);
    }