@@ -0,0 +1,1879 @@
+package test;%0A%0A/**%0Ascala%3E test.Test.print(test.Test.generate(5))%0A1 3 1 2 4%0A3 1 2 1 1%0A4 1 2 3 1%0A2 2 4 2 4%0A3 4 1 3 1%0A*/%0Apublic class Test %7B%0A  public static int%5B%5D%5B%5D generate(int n) %7B%0A    if (n %3C= 0 %7C%7C n %3E 100) throw new IllegalArgumentException(%22Invalid n %22 + n);%0A    int%5B%5D%5B%5D matrix = new int%5Bn%5D%5Bn%5D;%0A    generateRecur(matrix, n, n, 0, 0);%0A    return matrix;%0A  %7D%0A%0A  public static void print(int%5B%5D%5B%5D matrix) %7B%0A    for (int i = 0; i %3C matrix.length; i++) %7B%0A      for (int j = 0; j %3C matrix%5Bi%5D.length; j++) %7B%0A        System.out.print(matrix%5Bi%5D%5Bj%5D + %22 %22);%0A      %7D%0A      System.out.println();%0A    %7D%0A  %7D%0A%0A  private static boolean generateRecur(int%5B%5D%5B%5D matrix, int r, int c, int i, int j) %7B%0A    if (i %3C 0 %7C%7C j %3C 0 %7C%7C i %3E= r %7C%7C j %3E= c) return true;%0A    if (matrix%5Bi%5D%5Bj%5D != 0) return true;%0A    int%5B%5D arr = getRandomChoices();%0A    for (int k : arr) %7B%0A      matrix%5Bi%5D%5Bj%5D = k;%0A      if (isValid(matrix, r, c, i, j)) %7B%0A        boolean ok = generateRecur(matrix, r, c, i, j + 1) && generateRecur(matrix, r, c, i + 1, j);%0A        if (ok) return true;%0A      %7D%0A      matrix%5Bi%5D%5Bj%5D = 0;%0A    %7D%0A    return false;%0A  %7D%0A%0A  private static boolean isValid(int%5B%5D%5B%5D matrix, int r, int c, int i, int j) %7B%0A    int cnt = 1;%0A    for (int col = 1; col %3C c; col++) %7B%0A      if (matrix%5Bi%5D%5Bcol%5D != 0 && matrix%5Bi%5D%5Bcol%5D == matrix%5Bi%5D%5Bcol - 1%5D) %7B%0A        cnt++;%0A        if (cnt %3E 2) return false;%0A      %7D else %7B%0A        cnt = 1;%0A      %7D%0A    %7D%0A%0A    cnt = 1;%0A    for (int row = 1; row %3C r; row++) %7B%0A      if (matrix%5Brow%5D%5Bj%5D != 0 && matrix%5Brow%5D%5Bj%5D == matrix%5Brow - 1%5D%5Bj%5D) %7B%0A        cnt++;%0A        if (cnt %3E 2) return false;%0A      %7D else %7B%0A        cnt = 1;%0A      %7D%0A    %7D%0A%0A    return true;%0A  %7D%0A%0A  private static int%5B%5D getRandomChoices() %7B%0A    int%5B%5D arr = new int%5B4%5D;%0A    int rand = new java.util.Random().nextInt(43) %25 4;%0A    for (int i = 0; i %3C arr.length; i++) %7B%0A      arr%5Bi%5D = ((rand + i) %25 4) + 1;%0A    %7D%0A    return arr;%0A  %7D%0A%7D%0A

add careercup