> while(totalAttacks() != 0) {
if totalAttacks is never 0 then it would hang, perhaps you need an additional exit condition here
Main Topics
Browse All TopicsI have an n sized int array containing a permutation of 0...n. I use this to conceptualize an nXn chessboard, using the array index i for the rows of the board, and array[i] as the columns. So for a permutation of n = 4, I have, say, (2,1,0,3) , giving me a board with queens at (0,2),(1,1),(2,0),(3,3). This ensures that no two rows have a queen in the same column. I then call optimize() on this permutation, which checks whether there are attacks on the board (totalAttacks()). This total includes attack for queens a and b, for a to b and b to a, so a pair of attacking queens adds 2 to the count. numAttacks(int i) counts all attacks by the queen in the ith row, not including itself. swap(int i, int j) simply swaps the two elements at the given indices. so swap(0, 1) on the given permutation above gives me (1,2,0,3). fill() gives a new permutation if the current permutation cannot be optimized in this way. Below is the code for the optimize() method. It works pretty well for n < 15, but occasioinally hangs for n > 12, and always hangs for n = 5. Why does it hang for n = 5, why does it sometimes hang for other n, and what can I change to get this to work for n = 5 and larger (maybe up to n = 20)? Thanks in advance for your help -supr
public void optimize() {
int iathen, ianow, jathen, janow;
while(totalAttacks() != 0) {
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
iathen = numAttacks(i);
jathen = numAttacks(j);
if(iathen != 0 && jathen != 0) {
swap(i, j);
ianow = numAttacks(i);
janow = numAttacks(j);
if(iathen < ianow && jathen < janow)
swap(i, j);
}
}
if(i == n - 1)
fill();
}
}
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
The more I test this code, the more I'm convinced that n > 12 do eventually find an answer, it just takes a really long time. So I'm revising my question: Why can't this code find an acceptable permutation for n = 5? And, is there anything I can do with this algorithm to speed it up for larger n? Thanks -supr
objects-
I had never tried it with an explicit permutation, until now. I just checked it with permutation (0,2,4,1,3), and I get totalAttacks = 0, before I call optimize(). It does return the original permutation as the optimized one, so it is halting at totalAttacks = 0. But if I call it on a random permutation of n = 5, it still never halts. I'm going to include the whole Chessboard.java program, and the driver program, which may help you figure out whatever check I'm not making that keeps me in the while loop in optimize(). Thanks so far objects, I think we're gonna figure this baby out any time now. -supr
NQueens.java:
import java.io.*;
public class NQueens {
public static void main(String args []) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.i
while(true) {
System.out.print("Enter n, or zero to quit: ");
int n = Integer.parseInt(input.rea
if(n == 0)
System.exit(0);
Chessboard cb = new Chessboard(n);
cb.displayPerm();
for(int i = 0; i < n; i++)
System.out.print(cb.numAtt
System.out.println();
System.out.println(cb.tota
cb.displayBoard();
cb.optimize();
System.out.println(cb.tota
cb.displayPerm();
cb.displayBoard();
}
}
}
Chessboard.java:
public class Chessboard {
static int [] board;
static int n;
public Chessboard(int size) {
n = size;
board = new int[n];
fill();
}
public void usePerm(int a, int b, int c, int d, int e) {
board[0] = a;
board[1] = b;
board[2] = c;
board[3] = d;
board[4] = e;
}
private void fill() {
for(int i = 0; i < n; i++)
board[i] = i;
for(int j = 0; j < n; j++)
swap(j, (int)(Math.random() * j));
}
private void swap(int a, int b) {
int temp = board[b];
board[b] = a;
board[a] = temp;
}
public void displayPerm() {
for(int i = 0; i < n; i++)
System.out.print(board[i] + " ");
System.out.println();
}
public void displayBoard() {
String line = "";
for(int i = 0; i < n * 4 / 3 + 1; i++)
line += "---";
line += "\n";
for(int i = 0; i < n; i++) {
System.out.print("\n" + line + "| ");
for(int j = 0; j < n; j++)
if(board[i] == j)
System.out.print("Q | ");
else
System.out.print(" | ");
}
System.out.println("\n" + line);
System.out.println();
}
public int numAttacks(int i) {
int attacks = 0;
for(int j = 0; j < n; j++)
if(board[i]+i == board[j]+j || board[i]-i == board[j]-j)
attacks++;
return --attacks;
}
public int totalAttacks() {
int total = 0;
for(int i = 0; i < n; i++)
total += numAttacks(i);
return total;
}
public void optimize() {
int iathen, ianow, jathen, janow;
while(totalAttacks() != 0) {
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
iathen = numAttacks(i);
jathen = numAttacks(j);
if(iathen != 0 && jathen != 0) {
swap(i, j);
ianow = numAttacks(i);
janow = numAttacks(j);
if(iathen < ianow && jathen < janow)
swap(i, j);
}
}
if(i == n - 1)
fill();
}
}
}
}
Uh, I've just been printing out swaps and refills (new permutations), and I'm discovering that my swaps aren't doing anything, I'm just eventually refilling with the solution permutation. So, basically, my algorithm doesn't work at all, it simply halts when it randomly hits the right one. If anyone can help me figure out what it's doing wrong, I'd appreciate it -supr
Business Accounts
Answer for Membership
by: objectsPosted on 2005-02-22 at 18:13:59ID: 13378209
add some println() statement in your loop, they should give you a clue to why it it hanging,