您的位置:首页 > 其它

第1次实验——NPC问题(回溯算法、聚类分析)

2014-06-09 22:40 477 查看
(1)N皇后问题

public class Queen_Java {
int QUEEN_COUNT = 4; // 是多少皇后
static final int EMPTY = 0;
int[][] count = new int[QUEEN_COUNT][QUEEN_COUNT];
int[] QueenIndex = new int[QUEEN_COUNT];
int resultCount = 0;
long time = System.currentTimeMillis();

public void putQueenIndex(int row) {
for (int col = 0; col < QUEEN_COUNT; col++) {
if (count[row][col] == EMPTY) {
for (int iRow = row + 1; iRow < QUEEN_COUNT; iRow++) {
count[iRow][col]++;
if ((col - iRow + row) >= 0) {
count[iRow][col - iRow + row]++;
}
if ((col + iRow - row) < QUEEN_COUNT) {
count[iRow][col + iRow - row]++;
}
}
QueenIndex[row] = col;
if (row == QUEEN_COUNT - 1) {
print(++resultCount);
} else {
putQueenIndex(row + 1);
}
for (int iRow = row + 1; iRow < QUEEN_COUNT; iRow++) {
count[iRow][col]--;
if ((col - iRow + row) >= 0) {
count[iRow][col - iRow + row]--;
}
if ((col + iRow - row) < QUEEN_COUNT) {
count[iRow][col + iRow - row]--;
}
}
}
}
if (row == 0) {
System.out.println(QUEEN_COUNT + "皇后共有 " + resultCount + " 个解\n"
+ (System.currentTimeMillis() - time) + "毫秒");
}
}

public void print(int n) {
System.out.println(QUEEN_COUNT + "皇后的第 " + n + " 个解:");
for (int i = 0; i < QUEEN_COUNT; i++) {
for (int j = 0; j < QUEEN_COUNT; j++) {
System.out.print(QueenIndex[i] == j ? " * " : " - ");
}
System.out.println();
}
System.out.println();
}

public static void main(String[] args) {
new Queen_Java().putQueenIndex(0);
}
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: