如何在每次不多次使用同一行的情况下组合矩阵的元素

How to combine elements of a matrix without using the same row more than once each time

我的目标是组合这些元素(每个字母代表不同的字符串)

A;B;F;...
X;C;D;...
P;O;K;...
...

以各种可能的方式在文本框中,不重复也不组合同一行的元素。点代表延续。所以如果矩阵只是

A;B
X;C

结果应该是AX AC BX BC。如果是

A;B;F
X;C;D
P;O;K

结果将是 AXP AXO AXK ACP ACO ACK ADP ADO ADK BXP ....

我找到了一个算法

private void buildAllCombinationsRecursive<TSource>(IList<TSource> i_targetList, IList<TSource> i_sourceList, int i_currentPos)
{
    if (i_currentPos == i_targetList.Count)
    {
        string combination = "";
        for (int i = 0; i < i_targetList.Count; i++)
        {
            combination += i_targetList[i] + " ";
        }

        Console.WriteLine(combination);
        return;
    }
    for (int i = 0; i < i_sourceList.Count; i++)
    {
        i_targetList[i_currentPos] = i_sourceList[i];
        this.buildAllCombinationsRecursive(i_targetList, i_sourceList, i_currentPos + 1);
    }
}

但是它创建了我不需要的 ABC CBA 等。

查看 Eric Lippert 的 C# Cartesian product Recursion

你的每一行 'matrix' 都是递归步骤中的一个序列。

累加器函数将是字符串连接。尽管如此,为了性能起见,您可以选择 StringBuilder.Append.

Example on rextester