通过重新排序和翻转,矩形的所有独特组合

时间:2017-09-01 09:42:15

标签: c# algorithm math combinations permutation

我们给出了N个不同大小的矩形框架(允许重复)。编写一个程序,找到我们可以通过交换订单并翻转它们来重新排序这些框架的所有独特方式。

输入:

3
2 3
2 2
3 2

输出:

12
(2, 2) | (2, 3) | (2, 3)
(2, 2) | (2, 3) | (3, 2)
(2, 2) | (3, 2) | (2, 3)
(2, 2) | (3, 2) | (3, 2)
(2, 3) | (2, 2) | (2, 3)
(2, 3) | (2, 2) | (3, 2)
(2, 3) | (2, 3) | (2, 2)
(2, 3) | (3, 2) | (2, 2)
(3, 2) | (2, 2) | (2, 3)
(3, 2) | (2, 2) | (3, 2)
(3, 2) | (2, 3) | (2, 2)
(3, 2) | (3, 2) | (2, 2)

到目前为止,这是我在C#中的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Frames
{
    class Program
    {
        static void Main()
        {
            var n = int.Parse(Console.ReadLine());
            var frames = new Tuple<int, int>[n * 2];

            for (int i = 0; i < n * 2; i += 2)
            {
                var strs = Console.ReadLine().Split(' ');
                frames[i] = new Tuple<int, int>(int.Parse(strs[0]), int.Parse(strs[1]));
                frames[i + 1] = new Tuple<int, int>(frames[i].Item2, frames[i].Item1);
            }

            Array.Sort(frames);

            Recursion(frames, 0, n, new Tuple<int, int>[n], new bool[frames.Length]);
        }

        static void Recursion(Tuple<int, int>[] frames, int position, int n, Tuple<int, int>[] current, bool[] used)
        {
            if (position == n)
            {
                Console.WriteLine(string.Join(" | ", (IEnumerable<Tuple<int, int>>)current));
                return;
            }

            for (int i = 0; i < frames.Length - 1; i++)
            {
                if (used[i])
                {
                    continue;
                }
                used[i] = true;
                current[position] = frames[i];
                Recursion(frames, position + 1, n, current, used);
                used[i] = false;
            }
        }
    }
}

我似乎无法弄清楚如何处理所使用的帧的两个旋转,然后排除重复。 任何指示都将不胜感激。

1 个答案:

答案 0 :(得分:0)

感谢有关hashset的说明。这是工作解决方案。

using System;
using System.Collections.Generic;

namespace Frames
{
    class Program
    {
        static SortedSet<string> output = new SortedSet<string>();

        static void Main()
        {
            var n = int.Parse(Console.ReadLine());
            var frames = new Tuple<int, int>[n];

            for (int i = 0; i < n; i++)
            {
                var strs = Console.ReadLine().Split(' ');
                frames[i] = new Tuple<int, int>(int.Parse(strs[0]), int.Parse(strs[1]));
            }

            Recursion(frames, 0, new Tuple<int, int>[n], new bool[n]);

            Console.WriteLine(output.Count);
            Console.WriteLine(string.Join(Environment.NewLine, output));
        }

        static void Recursion(Tuple<int, int>[] frames, int position, Tuple<int, int>[] current, bool[] used)
        {
            if (position == frames.Length)
            {
                output.Add(string.Join(" | ", (IEnumerable<Tuple<int, int>>)current));
                return;
            }

            for (int i = 0; i < frames.Length; i++)
            {
                if (used[i])
                {
                    continue;
                }

                used[i] = true;

                current[position] = frames[i];
                Recursion(frames, position + 1, current, used);

                current[position] = new Tuple<int, int>(frames[i].Item2, frames[i].Item1);
                Recursion(frames, position + 1, current, used);

                used[i] = false;
            }
        }
    }
}