2

I've been playing around this code for awhile. The main Thing i'm trying to do is experiment with the output. Here is the source of the code: 1

The java code they used is as follows:

public class Queens {

   /***********************************************************************
    * Return true if queen placement q[n] does not conflict with
    * other queens q[0] through q[n-1]
    ***********************************************************************/
    public static boolean isConsistent(int[] q, int n) {
        for (int i = 0; i < n; i++) {
            if (q[i] == q[n])             return false;   // same column
            if ((q[i] - q[n]) == (n - i)) return false;   // same major diagonal
            if ((q[n] - q[i]) == (n - i)) return false;   // same minor diagonal
        }
        return true;
    }

   /***********************************************************************
    * Print out N-by-N placement of queens from permutation q in ASCII.
    ***********************************************************************/
    public static void printQueens(int[] q) {
        int N = q.length;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (q[i] == j) System.out.print("Q ");
                else           System.out.print("* ");
            }
            System.out.println();
        }  
        System.out.println();
    }


   /***********************************************************************
    *  Try all permutations using backtracking
    ***********************************************************************/
    public static void enumerate(int N) {
        int[] a = new int[N];
        enumerate(a, 0);
    }

    public static void enumerate(int[] q, int n) {
        int N = q.length;
        if (n == N) printQueens(q);
        else {
            for (int i = 0; i < N; i++) {
                q[n] = i;
                if (isConsistent(q, n)) enumerate(q, n+1);
            }
        }
    }  


    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);
        enumerate(N);
    }

}

My question is this: I was wondering how to make the output more streamlined. In other words, how can I make the output so that I have the row positions of queens listed in a set of parentheses for each possible solution? For example, Say I have a 4x4 board that looks like this:

xxQx
Qxxx
xxxQ
xQxx

and instead of having the above output, I want something like: (2,4,1,3), where "2" exemplifies the queen in the second row (the first one read when looking from right to left), "4" represents the queen in the 4th row, etc.. Since I'm relatively new to programming, I'm not too sure how to go about this. This is what I had actually tried in the printQueens part of the code:

public static void printQueens(int[] q) {
    int N = q.length;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (q[i] == j)
                int[] rowPos = new int[1+j];
                System.out.print(Arrays.toString(rowPos));

        }

        System.out.println();

    }
    System.out.println();
}

As you can see, under the "if" statement I made a new array I hoped would contain the row numbers in which the queens were in, then I wanted to print that out as a string. I am getting errors on this, though, and I have no idea why. I know the output I'm trying to get isn't going to look like (x,y,z), for instance, but I'm just trying to print the rows where the Queens are at this point, then worry about the commas and parentheses later. However, if you can help me with any of that, it would be great. Here's how the errors looked like:

https://i.stack.imgur.com/Ydlfk.png

EDIT: I made the necessary changes, but now my output looks like this: 1

Not sure why it's printing it based on the number of rows the arrays has. I just need one output per solution showing the row number of the queens

ani ben
  • 145
  • 6

1 Answers1

2
if (q[i] == j)
    int[] rowPos = new int[1+j];
    System.out.print(Arrays.toString(rowPos));

If this is supposed to be an if block, you need to use braces.

if (q[i] == j) {
    int[] rowPos = new int[1+j];
    System.out.print(Arrays.toString(rowPos));
}

But your rowPos array will always just hold zeroes because you don't put anything in it.

Edit:

If you just want to print the contents of q, which seems to be what you're saying, you can get rid of the loops and just do this:

public static void printQueens(int[] q) {
    System.out.println(Arrays.toString(q));
}

If you need want to print the positions as if they are indexed from one instead of zero, you could do it like this:

public static void printQueens(int[] q) {
    int[] adjusted = new int[q.length];
    for (int i = 0; i < q.length; ++i) {
         adjusted[i] = q[i] + 1;
    }
    System.out.println(Arrays.toString(adjusted));
}
khelwood
  • 55,782
  • 14
  • 81
  • 108