Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: N queens problem! Please help !I don't really know what the f... is going on ?

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default N queens problem! Please help !I don't really know what the f... is going on ?

    I need to solve the N queens problem (place n queens on a chess board size nxn so that the queens don't attack each other). Consist of 4 class :

    Queen.java : hold the row and column of the queen

    Stack.java : a data structure stack to hold the queen.

    Nqueens.java : to solve the problem

    testN.java : to test the solution

    the error I got is <identifier> expected in the testN.java, I don't really know what is going on, everything seems normal so far.

    Here are the codes:

    public class Queen{
     
      int row;
      int column;
     
      public Queen(int r,int c){
      row=r;
      column=c;
      } 
     
      public int getRow(){
      return row;
      }
     
      public int getCol(){
      return column;
      }
     
      public boolean isConflict(Queen q)
      {
      boolean ans= false;
      //row and collum conflict
      if(row==q.row||column==q.column) ans=true;
      //same diagonal if the vector creats by two queens has same absolute value for x and y coordinator.
      else if(Math.abs(row-q.row)==Math.abs(column-q.column)) 
        ans=true;
      return ans;
      }
     
      public String toString(){
      String ans="";
      ans=ans+"( "+row+" , "+column+" )";
      return ans;
      }
     
    }


    public class Stack
    {
    private Queen[] data;
    private int count;
     
    public  Stack(int n){
      data= new Queen[n];
     }
     
    public void push(Queen item)
    {
      if(count==data.length) increaseSize();
    data[count]=item;
    count++;
    }
     
    public int getSize(){
    return count;
    }
     
    public Queen pop(){
    Queen ans= null;
    if(count !=0)ans =data[count-1];
    count--;
    return ans;
    }
     
    public Queen lookitem(int i){
    Queen ans=null;
    if(count !=0)ans =data[i];
    return ans;
    }
     
    public Queen peek()
    {
    Queen ans= null;
    if(count !=0)ans =data[count-1];
    return ans;
    }
     
    public void increaseSize(){
    Queen[] temp=new Queen[2*data.length+1]; 
     
     
    for(int i=0;i<data.length;i++)
    {
    temp[i]=data[i];
    }
     
    data=temp;
    }
     
     
    }


    public class Nqueens{
     
    private Stack s;
    private int N;
     
    public Nqueens(int size)
    {
    s=new Stack(size); 
    N=size;  
    }
     
    //Solve the nqueens problem
    public void solution()
    {
     
    s.push(new Queen(1,1));s.push(new Queen(2,1));
     
    boolean solve=false;
     
    while(s.getSize()!=0&&solve==false)
    {
    boolean conflict=false;int i=0;
    while(i<s.getSize()-1&&conflict==false)
    {
    conflict=s.peek().isConflict(s.lookitem(i));  
    i++;      
    }
    if (conflict=false)
    {
    while(s.getSize()!=0&&s.peek().getRow()==N)
    {
    s.pop();
    }
    if(s.getSize()!=0)
    {
    Queen qq=new Queen(s.peek().getRow(),s.peek().getCol()+1);  
    s.pop();
    s.push(qq);
    }
    }
    else if(s.peek().getRow()==N) solve=true;
    else s.push(new Queen(s.peek().getRow()+1,1));
    }
     
    }
     
    public String toString(){
      String ans="";
        for(int i=0;i<s.getSize();i++){
        ans=ans+s.lookitem(i).toString()+"  ";
     
    }
    return ans;
    }}


    public class testN{
      public static void main(String arg[]){
     
        Queen q1=new Queen(1,1);
        Queen q2=new Queen(2,2);
        Queen q3=new Queen(2,1);
        Queen q4=new Queen(4,3);
        System.out.println(q1);
        System.out.println(q2);
        System.out.println(q1.isConflict(q4));
         System.out.println(q4.isConflict(q3));
      Stack s=new Stack(0);
      s.push(q1);s.push(q3);
      s.push(new Queen(3,6));
      System.out.println("Element on s "+s.peek());
      } 
     
      Nqueens qq = new Nqueens(4);
    qq.solution();
     
      System.out.println(qq);  
     
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: N queens problem! Please help !I don't really know what the f... is going on ?

    You have some code in your testN class which isn't inside of any method (last 3 lines).

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    bruce88 (October 12th, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: N queens problem! Please help !I don't really know what the f... is going on ?

    Quote Originally Posted by helloworld922 View Post
    You have some code in your testN class which isn't inside of any method (last 3 lines).
    Sorry I don't know what you mean ?

    I created a object qq which belongs to the Nqueens class. Then use object qq to activate the method solution ( method solution in the Nqueens class).

    Then I want to print out the result using toString() method.

  5. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: N queens problem! Please help !I don't really know what the f... is going on ?

    public class Sample extends Nothing {
       int myIntVar;
       double myDoubleVar = 4.0;
     
       public static void main(String[] args) {
          doSomeWork();
          doSomeOtherWork();
       }
     
       //random lines of code do not belong here!!
    }
    But you can have other methods....
    public class Sample extends Nothing {
       int myIntVar;
       double myDoubleVar = 4.0;
     
       public static void main(String[] args) {
          doSomeWork();
          doSomeOtherWork();
       }
     
       //random lines of code do not belong here!!
     
       public static void someLegalCode() {
          doDifferentWork();
       }
    }
    Last edited by jps; October 12th, 2012 at 02:21 PM. Reason: Fixed code missing }

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: N queens problem! Please help !I don't really know what the f... is going on ?

      }  // <-- end of main method
     
      Nqueens qq = new Nqueens(4); // valid declaration and initialization of object field, but I don't think this is what you wanted
    qq.solution(); // code isn't inside of a method, Java doesn't allow this
     
      System.out.println(qq);   // code isn't inside of a method, Java doesn't allow this
     
    }

    The solution would be to move your code inside the main method.

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    bruce88 (October 12th, 2012)

  8. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: N queens problem! Please help !I don't really know what the f... is going on ?

    ok i fixed it , thanks a lot. i know what you mean now.

Similar Threads

  1. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  2. Recursion for N Queens problem
    By dubois.ford in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 23rd, 2011, 09:46 PM