Member Avatar for Gsterminator
Hey everyone, 
    I am making a nqueen program that’ll read me an array of the position of the 
    queen. I developed to different methods to do this. One is the recursion way 
    and the other is the iteration method . My problem lies in the recursion. I am trying 
    to count and display the number of solutions that the method finds, but I simply can’t
    figure out how to do that because the count is inside a loop and once I make a 
    recursive call the method starts the count all over again. PLEASE HELP!!


package queenproblem;

import java.util.Scanner;
import java.util.*;
import java.util.Arrays;


public class Queenproblem {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        System.out.print("n = ");
        int n = kb.nextInt();
        int []a = new int[n];
        System.out.println("recursion...");
        rNqueen(0,a);

        System.out.println("iteration...");
        iNqueen(n, a);
    }
    public static boolean aPlace(int k , int[] x){
        for(int i = 0; i < k; i++){
            if((x[i] == x[k])|| (Math.abs(x[i]- x[k])== Math.abs(i - k)))
                return false;
        }
        //count(n);
        return true;
    }
    public static void rNqueen(int k, int [] x){
        int count = k;
        for(int i = 0; i< x.length; i++){
            x[k]= i+1;

            if(aPlace(k,x)){

                if(k == (x.length-1)){
                    count = x.length -count;
                    System.out.print(count + " = ");
                    System.out.println(Arrays.toString(x));
                }
                else{
                 //System.out.println("qcount =" + qcount);
                       rNqueen(k+1,x);
                }
            }
        }
    }
    public static void iNqueen(int n, int []x){
        int k2= 0;
        x[k2]= 0;
        int qcount= 0;
        while(k2 != -1){
            x[k2] = x[k2] + 1;
            while((x[k2] <= n)&& (!aPlace(k2, x))){
                x[k2]++;}
            if(x[k2]<= n){
                if(k2 == n-1){
                    qcount++;
                    System.out.print(qcount + " = ");
                    System.out.println(Arrays.toString(x));
                }
                else{
                     k2++;
                    x[k2]= 0;
                }  
            }
            else
                k2--;
        }
        System.out.println("There are "+ qcount + " solutions.");
    }
    public int countNqueen(int x){
        return x+1;
    }
}

Recommended Answers

All 2 Replies

uhm you use count(n) however i see no method for this where is the method? because if you are incrementing a simple int you would use variable++? where is count defined? it cannot be defined within the method that is recursively called thats the key. So either make it global or pass as a parameter to the method which increments count++

see pseudo for correct way:

class X {
int count=0;
main() {
for(i<4;i++) {
methodIncrementingCount();
print(count);//display 4 
}
}
methodIncrementingCount() {
count++
}
}
Member Avatar for Gsterminator

wow that was it!! Thanks so much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.