N Queen Problem

Given a chess board having N×N N*N cells, you need to place N queens on the board in such a way that no queen attacks any other queen.

Example:

Input:  4
Output: 0 0 1 0 
        1 0 0 0 
        0 0 0 1 
        0 1 0 0 

Approach

Java

public class NQueen {
    public static void main(String[] args) {
        int n = 4;
        int board[][] = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++)
                board[i][j] = 0;
        }
        N_Queen(board, 0, n);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();

        }

    }

    // check for the current cell
    // is safe or not
    static boolean isSafe(int board[][], int rowint colint n) {
        int ij;
        for (i = 0; i < col; i++)
            if (board[row][i] == 1)
                return false;
        for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
            if (board[i][j] == 1)
                return false;
        for (i = row, j = col; j >= 0 && i < n; i++, j--)
            if (board[i][j] == 1)
                return false;
        return true;

    }

    // function to check if N queeen can be placed or not
    // and find where they are places
    static boolean N_Queen(int board[][], int colint n) {
        if (col >= n)
            return true;
        for (int i = 0; i < n; i++) {
            // if safe then mark it as place for
            // queen
            if (isSafe(board, i, col, n)) {
                board[i][col] = 1;
                if (N_Queen(board, col + 1, n))
                    return true;
                // backtrack
                board[i][col] = 0;
            }
        }
        return false;
    }
}

C++

#include <bits/stdc++.h>
using namespace std;
#define N 10

//check for the current cell
//is safe or not
bool is_safe(int board[][N],int row,int col,int n)
{
   int i,j;
   for(i=0;i<col;i++)
      if(board[row][i])
         return false;
   for(i=row,j=col;i>=0&&j>=0;i--,j--)
         if(board[i][j])
            return  false;
   for(i=row,j=col;j>=0&&i<n;i++,j--)
       if(board[i][j]) 
          return false;
    return true;
    
}

//function to  check if N queeen can be placed or not
//and find where they are places
bool N_Queen(int board[][N],int col,int n)
{
    if(col>=n)
       return true;
    for(int i=0;i<n;i++)
      {
              
              //if safe then mark it as palce for
              //queen
              if(is_safe(board,i,col,n)==true)
                {
                    board[i][col]=1;
                  if(N_Queen(board,col+1,n)==true)
                   return true;
                 //backtrack
                board[i][col]=0;
                }
      }
    return false;
}
int main()
{
    int n=4;
    int board[10][10];
    for(int i=0;i<n;i++)
      {
          for(int j=0;j<n;j++)
              board[i][j]=0;
      }
      bool flag=N_Queen(board,0,n);
      for(int i=0;i<n;i++)
         {
             for(int j=0;j<n;j++)
              {
                  cout<<board[i][j]<<" ";
              }
              cout<<"\n";

         }
    return 0;
}


No comments:

Post a Comment