簡體   English   中英

C# 2D 數組排列

[英]C# 2D Array Permutations

我正在尋找一種方法來完成二維數組的所有可能排列。 我需要讓 Arrays 彼此保持伙伴關系,因為它們用於節省電線。

我能夠讓它與普通的一維數組一起工作,但我對如何處理二維數組感到有些困惑

一維數組排列的來源: https : //www.geeksforgeeks.org/heaps-algorithm-for-generate-permutations/

 //Array int[,] pos = { { 1, 2, 3 }, { 1, 2, 3 } }; //OUTPUT 1,1 2,2 3,3 2,2 1,1 3,3 3,3 1,1 2,2 1,1 3,3 2,2 2,2 3,3 1,1 3,3 2,2 1,1

我發現這里是未來任何人的代碼。

 using System; public class GFG { // Prints the array static void printArr(int[,] a, int n) { for (int i = 0; i < n / 2; i++) { Console.Write(a[0, i]); Console.Write(a[1, i] + " "); } Console.WriteLine(); } // Generating permutation using Heap Algorithm static void heapPermutation(int[,] a, int size, int n) { // if size becomes 1 then prints the obtained // permutation if (size == 1) printArr(a, n); for (int i = 0; i < size; i++) { heapPermutation(a, size - 1, n); // if size is odd, swap 0th ie (first) and // (size-1)th ie (last) element if (size % 2 == 1) { int temp = a[0,0]; a[0,0] = a[0,size - 1]; a[0,size - 1] = temp; temp = a[1,0]; a[1,0] = a[1,size - 1]; a[1,size - 1] = temp; } // If size is even, swap ith and // (size-1)th ie (last) element else { int temp = a[0,i]; a[0,i] = a[0,size - 1]; a[0,size - 1] = temp; temp = a[1,i]; a[1,i] = a[1,size - 1]; a[1,size - 1] = temp; } } } // Driver code public static void Main() { int[,] a = { { 1, 2, 3 }, { 1, 2, 3} }; heapPermutation(a, a.Length - (a.Length / 2), a.Length); } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM