Beispiel #1
0
        public void NRooksHarderTest()
        {
            var n = 8;

            var p = new Problem("N rooks hard")
            {
                Timeout = 2000000
            };


            // rook(i, j) means there's a root at row i, column j
            var rook = Predicate <int, int>("rook");

            //
            // Add constraint to the program
            //

            // There should be a total of n rooks
            p.Exactly(n, Range(0, n).SelectMany(i => Range(0, n).Select(j => rook(i, j))));

            // There should be at most one rook in each row
            foreach (var i in Range(0, n))
            {
                p.AtMost(1, Range(0, n), j => rook(i, j));
            }

            // There should be at most one rook in each column
            foreach (var j in Range(0, n))
            {
                p.AtMost(1, Range(0, n), i => rook(i, j));
            }

            var m = p.Solve();

            //
            // Test the constraint in the solution
            //

            m.Exactly(n, Range(0, n).SelectMany(i => Range(0, n).Select(j => rook(i, j))));
            foreach (var i in Range(0, n))
            {
                m.AtMost(1, Range(0, n).Select(j => rook(i, j)));
            }
            foreach (var j in Range(0, n))
            {
                m.AtMost(1, Range(0, n).Select(i => rook(i, j)));
            }
        }