Random Latin squares: Difference between revisions
Content added Content deleted
(Added c#) |
|||
Line 21: | Line 21: | ||
* [http://oeis.org/A002860 OEIS A002860] |
* [http://oeis.org/A002860 OEIS A002860] |
||
<br> |
<br> |
||
=={{header|C#|C sharp}}== |
|||
{{trans|Kotlin}} |
|||
<lang csharp>using System; |
|||
using System.Collections.Generic; |
|||
namespace RandomLatinSquares { |
|||
using Matrix = List<List<int>>; |
|||
// Taken from https://stackoverflow.com/a/1262619 |
|||
static class Helper { |
|||
private static readonly Random rng = new Random(); |
|||
public static void Shuffle<T>(this IList<T> list) { |
|||
int n = list.Count; |
|||
while (n > 1) { |
|||
n--; |
|||
int k = rng.Next(n + 1); |
|||
T value = list[k]; |
|||
list[k] = list[n]; |
|||
list[n] = value; |
|||
} |
|||
} |
|||
} |
|||
class Program { |
|||
static void PrintSquare(Matrix latin) { |
|||
foreach (var row in latin) { |
|||
Console.Write('['); |
|||
var it = row.GetEnumerator(); |
|||
if (it.MoveNext()) { |
|||
Console.Write(it.Current); |
|||
} |
|||
while (it.MoveNext()) { |
|||
Console.Write(", "); |
|||
Console.Write(it.Current); |
|||
} |
|||
Console.WriteLine(']'); |
|||
} |
|||
Console.WriteLine(); |
|||
} |
|||
static void LatinSquare(int n) { |
|||
if (n <= 0) { |
|||
Console.WriteLine("[]"); |
|||
return; |
|||
} |
|||
var latin = new Matrix(); |
|||
for (int i = 0; i < n; i++) { |
|||
List<int> temp = new List<int>(); |
|||
for (int j = 0; j < n; j++) { |
|||
temp.Add(j); |
|||
} |
|||
latin.Add(temp); |
|||
} |
|||
// first row |
|||
latin[0].Shuffle(); |
|||
// middle row(s) |
|||
for (int i = 1; i < n - 1; i++) { |
|||
bool shuffled = false; |
|||
while (!shuffled) { |
|||
latin[i].Shuffle(); |
|||
for (int k = 0; k < i; k++) { |
|||
for (int j = 0; j < n; j++) { |
|||
if (latin[k][j] == latin[i][j]) { |
|||
goto shuffling; |
|||
} |
|||
} |
|||
} |
|||
shuffled = true; |
|||
shuffling: { } |
|||
} |
|||
} |
|||
// last row |
|||
for (int j = 0; j < n; j++) { |
|||
List<bool> used = new List<bool>(); |
|||
for (int i = 0; i < n; i++) { |
|||
used.Add(false); |
|||
} |
|||
for (int i = 0; i < n-1; i++) { |
|||
used[latin[i][j]] = true; |
|||
} |
|||
for (int k = 0; k < n; k++) { |
|||
if (!used[k]) { |
|||
latin[n - 1][j] = k; |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
PrintSquare(latin); |
|||
} |
|||
static void Main() { |
|||
LatinSquare(5); |
|||
LatinSquare(5); |
|||
LatinSquare(10); // for good measure |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre>[3, 0, 1, 4, 2] |
|||
[4, 2, 3, 1, 0] |
|||
[2, 1, 4, 0, 3] |
|||
[0, 4, 2, 3, 1] |
|||
[1, 3, 0, 2, 4] |
|||
[4, 0, 2, 3, 1] |
|||
[2, 1, 4, 0, 3] |
|||
[0, 4, 3, 1, 2] |
|||
[3, 2, 1, 4, 0] |
|||
[1, 3, 0, 2, 4] |
|||
[5, 9, 4, 2, 0, 3, 8, 6, 1, 7] |
|||
[7, 1, 3, 9, 6, 2, 4, 0, 8, 5] |
|||
[1, 8, 6, 3, 4, 0, 9, 5, 7, 2] |
|||
[9, 0, 1, 4, 2, 7, 3, 8, 5, 6] |
|||
[6, 7, 8, 0, 5, 1, 2, 3, 9, 4] |
|||
[8, 2, 7, 5, 3, 4, 1, 9, 6, 0] |
|||
[2, 4, 5, 6, 8, 9, 0, 7, 3, 1] |
|||
[4, 5, 9, 8, 1, 6, 7, 2, 0, 3] |
|||
[0, 3, 2, 7, 9, 5, 6, 1, 4, 8] |
|||
[3, 6, 0, 1, 7, 8, 5, 4, 2, 9]</pre> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |