/// G. Hagopian generating a random latin square

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<iomanip>

using namespace std;

class LatinSquare {
    int sz;
    int* Ls = new int[sz*sz];
public:
    LatinSquare(int s) : sz(s) {
        for(int i = 0; i < sz; ++i)
            for(int j = 0; j < sz; ++j)
                Ls[i*sz+j]=((i*sz+j)%sz+i)%sz+1;
    }
    void print() {
        for(int i = 0; i < sz*sz; ++i) {
            cout << setw(3) << Ls[i];
            if((i+1)%sz == 0) cout << endl;
        }
    }
    void shuffleRows() {
        /** Use fisher-yates
        -- To shuffle an array a of n elements (indices 0..n-1):
        for i from n−1 downto 1 do
            j ← random integer such that 0 ≤ j ≤ i
        exchange a[j] and a[i]*/
        int k{0};
        for(int i = sz-1; i > 0; --i) {
            k = rand()%i;
            for(int col = 0; col < sz; col)
                /// swap ith row with kth row col by col
                swap(Ls[sz*i+col], Ls[sz*k+col]);
        }
    }

};

int main() {
    srand(time(0));
    LatinSquare latsq(9);
    latsq.print();
    return 0;
}
