62,612
社区成员
发帖
与我相关
我的任务
分享
class LaDing {
final int n;
int[][] m;
int count = 0;
LaDing(int n) {
this.n = n;
m = new int[n][n];
fill(0);
}
boolean check(int i, int j, int a) {
for(int col=0; col<j; col++)
if(m[i][col] == a) return false;
for(int row=0; row<i; row++)
if(m[row][j] == a) return false;
return true;
}
void fill(int i) {
int row = i/n;
int col = i%n;
for(int a=1; a<=n; a++) {
if(check(row, col, a)) {
m[row][col] = a;
if(i == n*n-1) {
count++;
System.out.println(count);
for(int[] line : m) {
for(int x : line)
System.out.print(x);
System.out.println();
}
System.out.println("================");
}
else fill(i+1);
}
}
}
public static void main(String[] args) {
new LaDing(3);
}
}
class LaDing {
private int n = 1;
public LaDing(int n) {
this.n = n;
}
public int get(int row, int col) {
return (row + col) % n + 1;
}
public void print() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(get(i, j) + " ");
}
System.out.println();
}
}
}
class LaDing {
private int[] num = null;
public LaDing(int n) {
num = new int[n];
for (int i = 0; i < num.length; i++) {
num[i] = i + 1;
}
}
public int get(int row, int col) {
return num[(row + col) % num.length];
}
public void print() {
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num.length; j++) {
System.out.print(get(i, j) + " ");
}
System.out.println();
}
}
}