# 8 queens 1 dimensional array

Page 1 of 1

## 6 Replies - 370 Views - Last Post: 21 January 2013 - 11:56 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=308250&amp;s=c646ff1e969d7dd59971fcd579fa34a0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jwhiteknight

Reputation: 0
• Posts: 14
• Joined: 09-January 13

# 8 queens 1 dimensional array

Posted 20 January 2013 - 05:27 PM

I have to write the program 8 queens. where the out put looks like
Q******
**Q****
****Q**
******Q
*Q*****

I have to print out all 92 solutions.
I am not even sure if I am on the right track.

I get this error message when I go to compile.
error: method displayBoard in class EightQu eens cannot be applied to given types;
displayBoard();
^
required: int[]
found: no arguments
reason: actual and formal argument lists differ in length

```public class EightQueens
{
private final int size = 8;
int [] board = new int [size];

public  void main (String args []) // main method
{
int  y =0;
board[0] = 1;
while (y >=0)
{
board[y]++;
}
while ((board[y]< 8) && (solveQueens(y)));
if (board[y] < 8)
{
if( y < 7)
{
board[++y]=1;
}
else
{
displayBoard();
}
}
}//  end main

private  boolean solveQueens(int y)
{
int x = board[y];

for (int i = 1;i < y; i++)
{
int t = board[y-1];
if (t==x || t==x - i || t ==x +1)
{
return true;
private void displayBoard (int[] board)
{
int A =board.length; // sets the lenght for the loops
for ( int i =0; i < A; i++)
{
for (int j=0; j < A; j++)
{
if (board[A] == j)
System.out.print("Q ");
else
System.out.print("* ");
}
System.out.println();
}
System.out.println();
}
}

```

ps.
is there an easy way to copy and past my code from linux vim to this site. with out duplicating the code that is in the vim?

Is This A Good Question/Topic? 0

## Replies To: 8 queens 1 dimensional array

### #2 ianian112

• D.I.C Regular

Reputation: 118
• Posts: 378
• Joined: 28-November 09

## Re: 8 queens 1 dimensional array

Posted 20 January 2013 - 06:23 PM

sorry if you find this rude, but have you ever programmed in Java before?

Your method expects an array, but you call it without one on line 24.

### #3 NathanMullenax

Reputation: 81
• Posts: 176
• Joined: 23-September 12

## Re: 8 queens 1 dimensional array

Posted 20 January 2013 - 06:23 PM

The error is pretty straightforward. displayBoard needs an argument--an array of integers.

As far as the algorithm is concerned, you could use backtracking, but you should be aware of how much space and time that would take up. You can reduce the amount of space needed by coming up with a better board representation and keeping track of symmetry.

There are actually only 12 'fundamental' solutions; if you find these twelve, you can generate all of the others by rotating, reflecting, and transposing that set of twelve.

You could actually encode the whole board in 3 bytes if you observe that it only takes three bits to describe the position of a queen within a single row.

Hope this helps.

This post has been edited by NathanMullenax: 20 January 2013 - 06:25 PM

### #4 NathanMullenax

Reputation: 81
• Posts: 176
• Joined: 23-September 12

## Re: 8 queens 1 dimensional array

Posted 20 January 2013 - 06:32 PM

Pseudo-spoiler. Here's how I did it in C++. It could certainly be improved, but it works and it is pretty fast.
Spoiler

### #5 jwhiteknight

Reputation: 0
• Posts: 14
• Joined: 09-January 13

## Re: 8 queens 1 dimensional array

Posted 20 January 2013 - 10:18 PM

ianian112, on 20 January 2013 - 06:23 PM, said:

sorry if you find this rude, but have you ever programmed in Java before?

Your method expects an array, but you call it without one on line 24.

no i don't find it very rude.. I am very new to java I am just starting my second java class. I would rather be learning MCSA... but I am trying my best

### #6 macosxnerd101

• Self-Trained Economist

Reputation: 9037
• Posts: 33,523
• Joined: 27-December 08

## Re: 8 queens 1 dimensional array

Posted 20 January 2013 - 11:38 PM

### #7 jwhiteknight

Reputation: 0
• Posts: 14
• Joined: 09-January 13

## Re: 8 queens 1 dimensional array

Posted 21 January 2013 - 11:56 AM

fix the code, so i don't get any errors, when I go and compile the code, but when I go to run the program I don't get any out put I think I am stuck in some sore of loop and it is not getting to the printboard method.
```public class EightQueens
{
public static  void main (String args []) // main method
{
int size = 8;
int [] board = new int [size];
int  y =0;
board[0] = 1;
while (y >=0)
{
board[y]++;
}
while ((board[y]< 8) && (solveQueens( y, board)))// int [])));
if (board[y] < 8)
{
if( y < 7)
{
board[++y]=1;
}
else
{
displayBoard( board);
}
// else
// {
//   y--;

// }
}
}//  end main

private static boolean solveQueens(int y,int[] board)
{
int x = board[y];
//system.out.printf (" in method solvequeens %d",x);

for (int i = 1;i < x; i++)
{
int t = board[y-1];
if (t==x || t==x - i || t ==x +1)
{
return true;
}
}

```