So I have been programming for a couple of months and now my assignment is to write a code that makes a list (prints) all latin squares that satisfy certain conditions. This being programming at not such a high level, only up to 5x5 squares will be tested, but even in those my program runs for absurd amount of time. I got O(5th power of 5!).
Parallel to this generator of all latin squares task requires checking some aspects of them but that is not the problem as that comes down to couple of ifs checking certain aspects in certain conditions.
Can anyone please help me with that?
Latin square is an n × n array filled with n different symbols, each occurring exactly once in each row and exactly once in each column.
> my program runs for absurd amount of time
perhaps may be a good idea to show your code.
Consider the latin square
1 2 3 4 5
5 1 2 3 4
4 5 1 2 3
3 4 5 1 2
2 3 4 5 1
all (¿?) other latin squares may be obtained performing rows and column swapping.
Got 5! rows permutations and 5! columns permutations, so in total 5!**2 = 14400
Write the output to a file and it will run faster. Screen (console/cout) output for large amounts of data can take all day.
Between this and the above row/col swap approach, can you get it to execute quickly?
Hint.. you might be able to transpose the thing and repeat the row logic twice, and rows can be swapped with just a pointer or vector, maybe? There might be a full one transposition table approach that for every square you compute, you can rotate or mirror or something to get a second one "free". Even if you don't go there, just doing the brute force swaps, if you can avoid swapping individual values and stick to swapping groups, it should move right along.