8 queens problem

6.4k Views Asked by At

How can i go about implemting 8/4 queens problem?Should i use DFS/BFS,I think DFs will be better. Can any one give some pseudocode/guidlines?

4

There are 4 best solutions below

1
On BEST ANSWER

Use a stack and backtracking, easiest way is via recursion.

See these other SO posts:

Dumb 8 Queens problem in C++

0
On

My solution have 2 pre-defined logics, there is only one queen at row, and there is only one queen at column. There is an one-dimensional array that length is 8. All array value set one of the 0-7, but all values used exactly one time (permutation of values that 0-7) arr[0]=5 value means queen at column 6 at first row arr[1]=3 value means queen at column 4 at second row, just control cross violation values on array check for, there is no need for check line or row violation. Permutation and cross violation functions all you need, (C++ STL has permutation functions, that just need to cross violation functions)

0
On

If queens are at (i,j) and (k,l) coordinates,then they can attack each other if

  1. i=k (same row)
  2. j=l (same column)
  3. |i-k|=|j-l| (diagonally),| | denotes the absolute value

    bool place(k,i)
    {
    //returns true if the queen can be placed at k-th row and i-th column
    //x[] is a global array with first (k-1) values set already.
    //x[p]=q means a queen is at location (p,q)
    
    for(j=1 to k-1)
    {
    if(x[j]==i)||(ABS(x[j]-i)==ABS(j-k))  //checking if another queen in same column or     diagonally
    return false;
    }
    return true;
    }
    

    To print all possible placements using backtracking:

    void NQueens(k,n) {

    for(i=1 to n)
    {
    if(place(k,i)) //checking if queen can be placed at (k,i)
    {
    x[k]=i;
    if(k==n) then write (x[1:n]);  
    else Nqueens(k+1,n);
     }
     }
    }
    

*With reference from saurabh school

0
On

Here is my implementation using backtracking. Change the value of N to get the different solutions.

It will print all the solutions available for given number of queens.

package com.org.ds.problems;

public class NQueueProblem {
private static int totalSolution = 0;
    public static void main(String[] args) {
        int n = 5;
        int arr[][] = new int[n][n];
        backTrack(arr, 0);
        System.out.println("\nTotal Number of Solutions are:- " + totalSolution);
    }

    private static void printQueuens(int[][] arr) {
        totalSolution++;
        System.out.println("\n========Start Printing Solution "+totalSolution+"=========");
        for(int i=0; i<arr.length;i++) {
            for(int j=0; j<arr.length;j++) {
                if(arr[i][j] == 1)
                    System.out.print(" Q"+(i+1) + " |");
                else
                    System.out.print("    |");
            }
            System.out.println();
        }
    }

    private static boolean backTrack(int[][] arr, int row) {
        if (row < 0 || row >= arr.length)
            return true;

        for (int col = 0; col < arr.length; col++) {
            if (isAttacked(arr, row, col)) {
                arr[row][col] = 1;
                if (backTrack(arr, row + 1)) {
                    if(row == (arr.length-1)) {
                        printQueuens(arr);
                        arr[row][col] = 0;
                    }
                    else {
                        return true;    
                    }
                } else {
                    arr[row][col] = 0;
                }
            }
        }
        return false;
    }

    private static boolean isAttacked(int[][] arr, int row, int col) {
        if (row == 0)
            return true;
        // check for same row
        for (int i = 0; i < arr.length; i++) {
            if (arr[row][i] == 1)
                return false;
        }
        // check for same col
        for (int i = 0; i <= row; i++) {
            if (arr[i][col] == 1)
                return false;
        }
        // check for diagonal
        // a.) Left dia
        int i = row - 1;
        int j = col - 1;
        while (i >= 0 && j >= 0) {
            if (arr[i][j] == 1)
                return false;
            i--;
            j--;
        }
        // b.) right dia
        i = row - 1;
        j = col + 1;
        while (i >= 0 && j < arr.length) {
            if (arr[i][j] == 1)
                return false;
            i--;
            j++;
        }
        return true;
    }
}