6 Replies - 984 Views - Last Post: 21 January 2013 - 11:56 AM Rate Topic: -----

#1 jwhiteknight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#4 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 jwhiteknight  Icon User is offline

  • New D.I.C Head

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

Re: 8 queens 1 dimensional array

Posted 20 January 2013 - 10:18 PM

View Postianian112, 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
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,851
  • Joined: 27-December 08

Re: 8 queens 1 dimensional array

Posted 20 January 2013 - 11:38 PM

Make sure to properly indent and space your code as well to make it easier to read. It will help us better get a handle on your logic, and it will help you better read your code.
Was This Post Helpful? 1
  • +
  • -

#7 jwhiteknight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
}
    }



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1