Question

this is N queens problem that i try to solve ,but i have this problem of non-static method .. how can i solve it .....

*> at the count(int) method .. i don't understand how to solve this problem

error: non-static method count() cannot be referenced from a static context

import java.util.*;

 public class NQueens {
static int n;
int[][] board = new int[n][n]; 
public static void main(String[] args) {

    //int result;
    int col = 0;
    Scanner input=new Scanner(System.in);
    n=input.nextInt();
    if(n < 4)
        System.out.println("the result is 0");
    else
        count(0);

}

int cnt=0;
void  count(int col){
    if(col == n){
        cnt++;
        System.out.println("the result is " + cnt);
    }

    else{
        for(int row=0 ; row<n ; row++){
            if(placeQueen(row, col))
                count(col+1);
            else
                removeQueen(row , col);
        }
    }

}

boolean placeQueen(int row , int col){
    boolean x =false;
    if(validPlace(row , col)){
        setQueen(row , col);
        x = true;
    }
    return x;
}
boolean validPlace(int row ,int col){
    boolean x = false;
    if(board[row][col] != -1)
        x=true;
    return x;
}
void setQueen(int row , int col){
    board[row][col] = 1;
    killCell(row , col);
}
void killCell(int row , int col){
    for(int i=col+1 ; i<n ; i++)
        board[row][i] = -1;
    for(int k=col+1 ; k<n ; k++){
        for(int j=0 ; j<n ; j++)
            if(Math.abs(row-j) == Math.abs(col-k))
                board[j][k] = -1;
    }
}
void removeQueen(int row , int col ){
    board[row][col] = 0;
    refreshCell(row , col);
}
void refreshCell(int row , int col){
    for(int i =col+1 ; i<n ; i++)
        board[row][i]=0;
    for(int k=col+1 ; k<n ; k++){
        for(int j=0 ; j<n ; j++)
            if(Math.abs(row-j) == Math.abs(col-k))
                board[j][k]=0;
    }
}

}

Was it helpful?

Solution

Your main method is a static method and has no instance of your class until it creates one. You cannot call the count(int) method unless you have an instance of the class. Try this instead:

public static void main(String[] args) {

    //int result;
    int col = 0;
    Scanner input=new Scanner(System.in);
    n=input.nextInt();
    if(n < 4)
        System.out.println("the result is 0");
    else {
        NQueens nq = new NQueens();
        nq.count(n);
    }

}

Another problem is that your count(int) method tries to refer to the 'n' variable. It cannot because it's only existing in the static main method. You have to pass 'n' to the count method so it can see it. If you want to initialize the class with a starting value, perhaps you need a constructor that takes that value instead, like this:

public void NQueens(int starting_n) {
    start = starting_n;
}

Then in main you'd use NQueens nq = new NQueens(n); to create your object. You'd also have to define int start = 0; in your class so it has a place to store the value being passed in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top