这个算起来很快只有92种放法
package 难题.八皇后;
import java.util.Random;
public class TryRun {
Random r = new Random();
static int df[][][] = new int[200][10][10];
static int dfIndex = 0;
/**
* @param args
*/
public static void main(String[] args) {
while (true) {
Map.mapSet();
// Map.showMaps();
Queens Q = new Queens();
// for (int i = 0; i <
// Q.queensXY.length; i++) {
// for (int j = 0; j <
// Q.queensXY[i].length;
// j++) {
// System.out.print(Q.queensXY[i][j]
// + " ");
// }
// System.out.println();
// }
writeMap(Q.queensXY);
if (Q.scanDiagonal()) {
// Map.showMaps();
saveArrs(Map.map);
// break;
// System.out.println("不同");
}
// else {
// Map.showMaps();
// System.out.println("同了");
// break;
// }
}
}
static void writeMap(int[][] queensXY) {
for (int i = 0; i < 8; i++) {
Map.map[queensXY[0][i]][queensXY[1][i]] = 1;
}
}
static void saveArrs(int[][] map) {
if (dfIndex == 0) {
copyArr(map);
return;
}
if (checkSame(map)) {
return;
}
copyArr(map);
}
static void copyArr(int[][] map) {
for (int m = 0; m < map.length; m++) {
for (int n = 0; n < map[m].length; n++) {
df[dfIndex][m][n] = map[m][n];
}
}
Map.showMaps();
System.out.println("第" + (dfIndex + 1) + "种");
dfIndex++;
}
static boolean checkSame(int[][] map) {
boolean same = false;
checkSame: for (int k = 0; k < dfIndex; k++) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (map[i][j] != df[k][i][j]) {
same = false;
continue checkSame;
}
same = true;
}
}
if (same) {
return true;
}
}
return false;
}
/*
* static boolean scanRow() { // 同行
*
* for (int i = 1; i < 9; i++) { int
* count = 0; for (int j = 1; j < 9;
* j++)
* { if(Map.map[i][j]==1){ count++;
* } if(count==2){ return false; } }
* }
*
* // 同列
*
* for (int i = 1; i < 9; i++) { int
* count = 0; for (int j = 1; j < 9;
* j++)
* { if(Map.map[j][i]==1){ count++;
* } if(count==2){ return false; } }
* }
*
* // 斜线 /方向
*//**
*
for (int i = 0; i < 15; i++) {
* int count = 0; for (int j = 1; j
* < i /
* 2 + 1; j++) { if (Map.map[8 - i /
* 2 + j][j] == 1) { count++; } if
* (count
* == 2) { //
* System.out.println("A 同"); return
* false; } } }
*/
/*
* // 斜线 /方向
*
* for (int i = 0; i < 15; i++) {
* int count = 0; for (int j = 1; j
* < i / 2 +
* 1; j++) { if (Map.map[j][8 - i /
* 2 + j] == 1) { count++; } if
* (count ==
* 2) { //
* System.out.println("B 同"); return
* false; } } }
*
* // for (int i = 1; i < 9; i++) {
* // if (Map.map[i][i] == 1) { //
* count++;
* // } // if (count == 2) { //
* System.out.println("斜线有双"); //
* return false;
* // } // }
*
* return true; }
*/
}
package 难题.八皇后;
final class ScanRow {
boolean scanDiagonal() {
// 斜线 /方向 左
int x = 0;
int y = 0;
int[][] arr = new int[8][8];
int k = 8;
for (int i = 0; i < k; i++) {
int count = 0;
if (arr[y][i] == 1) {
count++;
}
if (count == 2) {
return false;
}
y++;
k--;
}
x = 1;
y = 0;
for (int i = 0; i < k - 1; i++) {
int count = 0;
if (arr[i][x] == 1) {
count++;
}
if (count == 2) {
return false;
}
k--;
x++;
}
// //////
return true;
}
}
package 难题.八皇后;
import java.util.Random;
public final class Queens {
int[][] xy = new int[2][8];
int[][] queensXY = new int[2][8];
int numQueenX;
int numQueenY;
Random r = new Random();
Queens() {
for (int i = 0; i < this.xy.length; i++) {
for (int j = 0; j < this.xy[i].length; j++) {
this.xy[i][j] = j + 1;
}
}
setQueenX();
setQueenY();
}
void setQueenX() {
int Rdx = this.r.nextInt(this.xy[0].length);
if (this.xy[0][Rdx] == 0) {
setQueenX();
} else {
this.queensXY[0][this.numQueenX] = this.xy[0][Rdx];
this.numQueenX++;
this.xy[0][Rdx] = 0;
}
if (this.numQueenX != 8) {
setQueenX();
}
}
void setQueenY() {
int Rdy = this.r.nextInt(this.xy[1].length);
if (this.xy[1][Rdy] == 0) {
setQueenY();
} else {
this.queensXY[1][this.numQueenY] = this.xy[1][Rdy];
this.numQueenY++;
this.xy[1][Rdy] = 0;
}
if (this.numQueenY != 8) {
setQueenY();
}
}
boolean scanDiagonal() {
// 斜线 /方向 左
for (int k = 0; k < 8; k++) {
int count = 0;
for (int x = 0, y = k; x < 8 - k; x++, y++) {
if (Map.map[y + 1][x + 1] == 1) {
count++;
}
if (count == 2) {
// System.out.println("测 斜1左");
return false;
}
}
}
// 斜线 /方向 右
for (int k = 0; k < 7; k++) {
int count = 0;
for (int x = k + 1, y = 0; y < 8 - k - 1; x++, y++) {
if (Map.map[y + 1][x + 1] == 1) {
count++;
}
if (count == 2) {
// System.out.println("测 斜1右");
return false;
}
}
}
// 斜线 /方向 左
for (int k = 7; k >= 0; k--) {
int count = 0;
for (int x = k, y = 0; x >= 0; x--, y++) {
if (Map.map[y + 1][x + 1] == 1) {
count++;
}
if (count == 2) {
// System.out.println("测 斜2左");
return false;
}
}
}
// 斜线 /方向 右
for (int k = 0; k < 7; k++) {
int count = 0;
for (int x = 7, y = k + 1; y < 8; x--, y++) {
if (Map.map[y + 1][x + 1] == 1) {
count++;
}
if (count == 2) {
// System.out.println("测 斜2右");
return false;
}
}
}
// //////
return true;
}
}
package 难题.八皇后;
public class Map {
static int map[][] = new int[10][10];
static int hight=map.length;
static int weight=map[0].length;
static void mapSet() {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
// 边界
if (i == 0 || i == map.length - 1 || j == 0
|| j == map[i].length - 1) {
map[i][j] = 100;
// 低级怪物
} else{
map[i][j]=0;
}
}
}
}
static void showMaps() {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (map[i][j] == 100) {
System.out.print("* ");
}
else if (map[i][j] == 1) {
System.out.print("& ");
}
else {
String step="` ";
// step=step+(" "+Integer.toString(map[i][j]));
// if(step.length()<3){
// step+=" ";
// }
System.out.print(step);
}
}
System.out.println();
}
}
}