コード例 #1
0
        static void Main()
        {
            int nq = 8;

            if (nq > 1)
            {
                for (var layout = new Permutation(nq); ; ++layout.Rank)
                {
                    TOP : for (int x1 = 1; x1 < nq; ++x1)
                    {
                        for (int x2 = 0; x2 < x1; ++x2)
                        {
                            if (x1 - x2 - Math.Abs(layout[x2] - layout[x1]) == 0)
                            {
                                if (layout.Backtrack(x1) >= 0)
                                {
                                    goto TOP;
                                }
                                else
                                {
                                    return;
                                }
                            }
                        }
                    }

                    Console.WriteLine(String.Join(" ", Enumerable.Range(0, nq).Select(x => ((char)('a' + x)).ToString() + (layout[x] + 1))));
                }
            }
        }
コード例 #2
0
        public void UnitPn_Backtrack1()
        {
            var pn1 = new Permutation(new int[] { 2, 1, 0 });
            var pn2 = new Permutation(new int[] { 2, 1, 0 });
            var pn3 = new Permutation(new int[] { 0, 1, 2 });
            var pn4 = new Permutation(new int[] { 0, 2, 1 });
            var pn5 = new Permutation(new int[] { 2, 3, 1, 0 });
            var pn6 = new Permutation(new int[] { 1, 4, 0, 2, 3 });

            int result1 = pn1.Backtrack(0);
            int result2 = pn2.Backtrack(0);
            int result3 = pn3.Backtrack(1);
            int result4 = pn4.Backtrack(1);
            int result5 = pn5.Backtrack(2);
            int result6 = pn6.Backtrack(1);

            Assert.AreEqual(-1, result1);
            Assert.AreEqual(-1, result2);
            Assert.AreEqual(1, result3);
            Assert.AreEqual(0, result4);
            Assert.AreEqual(0, result5);
            Assert.AreEqual(0, result6);

            Assert.AreEqual(new Permutation(new int[] { 0, 2, 1 }).Rank, pn3.Rank);
            Assert.AreEqual(new Permutation(new int[] { 1, 0, 2 }).Rank, pn4.Rank);
            Assert.AreEqual(new Permutation(new int[] { 3, 0, 1, 2 }).Rank, pn5.Rank);
            Assert.AreEqual(new Permutation(new int[] { 2, 0, 1, 3, 4 }).Rank, pn6.Rank);
        }
コード例 #3
0
        public void CrashPn_Backtrack_ArgumentOutOfRange2()
        {
            var pn = new Permutation(2);

            pn.Backtrack(2);
        }
コード例 #4
0
        public void CrashPn_Backtrack_InvalidOperation()
        {
            var pn = new Permutation(choices: 4, picks: 2);

            pn.Backtrack(1);
        }