包含8个皇后的java的代码的词条( 四 )


public int[][] chase = new int[demo.ROW][demo.ROW];
public demo() {
for (int i = 0; idemo.ROW; i++)
for (int j = 0; jdemo.ROW; j++)
chase[i][j] = 0;
}
public void copy(int[][] k, int[][] l) {
for (int i = 0; idemo.ROW; i++)
for (int j = 0; jdemo.ROW; j++)
l[i][j] = k[i][j];
}
public void changeChase(int[][] chase, int row, int i) {
for (int j = 1; jdemo.ROW; j++) {
chase[row][j] = 1;
chase[j][i] = 1;
}
for (int j = 1; jdemo.ROW; j++) {
if (row - j = 0i - j = 0)
chase[row - j][i - j] = 1;
if (row + j = 7i - j = 0)
chase[row + j][i - j] = 1;
if (row - j = 0i + j = 7)
chase[row - j][i + j] = 1;
if (row + j = 7i + j = 7)
chase[row + j][i + j] = 1;
}
chase[row][i] = 2;
}
public void putout(int[][] chase) {
for (int i = 0; idemo.ROW; i++) {
for (int j = 0; jdemo.ROW; j++)
System.out.print(chase[i][j] + " ");
System.out.println();
}
}
public void putQueen(int row, int[][] m) {
if (row == 8) {
System.out.println("this is the" + demo.N++ + "个答案:");
putout(m);
} else {
for (int i = 0; i8; i++) {
if (m[row][i] == 0) {
int[][] l = new int[demo.ROW][demo.ROW];
copy(m, l);
changeChase(l, row, i);
putQueen(row + 1, l);
}
}
}
}
public static void main(String[] Args) {
demo Q = new demo();
Q.putQueen(0, Q.chase);
}
}
关于8个皇后的java的代码和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

推荐阅读