Search
 
SCRIPT & CODE EXAMPLE
 

C

Implement N-Queen Problem

0  0  1  0 
 1  0  0  0 
 0  0  0  1 
 0  1  0  0
Comment

n queen problem

import java.util.*;
import java.io.*;
import java.lang.*;

class Gfg
{
    
    static final int N = 4;
    static int board[][] = { { 0, 0, 0, 0 }, 
                    { 0, 0, 0, 0 }, 
                    { 0, 0, 0, 0 }, 
                    { 0, 0, 0, 0 } };
    static void printSolution(int board[][]) 
    { 
        for (int i = 0; i < N; i++) { 
            for (int j = 0; j < N; j++) 
                System.out.print(" " + board[i][j] 
                                 + " "); 
            System.out.println(); 
        } 
    }
  
    static boolean isSafe(int row, int col) 
    { 
        int i, j; 
  
        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; 
    } 
  
    static boolean solveRec(int col) 
    { 
        if (col == N) 
            return true; 
  
        for (int i = 0; i < N; i++) {  
            if (isSafe(i, col)) { 
                board[i][col] = 1; 
  
                if (solveRec(col + 1) == true) 
                    return true; 
  
                board[i][col] = 0; 
            } 
        }
        return false;
    }
    
    static  boolean solve() 
    { 
  
        if (solveRec(0) == false) { 
            System.out.print("Solution does not exist"); 
            return false; 
        } 
  
        printSolution(board); 
        return true; 
    }
    
    public static void main(String args[])
    {
        solve(); 
    }
}
Comment

PREVIOUS NEXT
Code Example
C :: how to initiate pointer array with null in c 
C :: Trier lexicographiquement en c 
C :: google sheets transpose new line to table 
C :: c ausgabe von variablen 
C :: networkx remove attributes 
C :: function that reverses the content of an array of integers. 
C :: get file ligne count c 
C :: c michael 
C :: taking input and converting it to a string in c 
C :: program to merge two strings in c 
C :: anticonstitutionnellement 
C :: iulia vântur 
C :: user define 
C :: how to write 2d array from bin file in c 
C :: Returns number of values 
C :: float 
C :: function for 2d dynamic array 
C :: time random c 
C :: array length in c++ 
Dart :: flutter flotingactionbutton position 
Dart :: how to change input text color in flutter 
Dart :: how to get the name of the day in flutter 
Dart :: dart timestamp 
Dart :: taskkill dart 
Dart :: trailing flutter with 2 icons flutter 
Dart :: fix overflow flutter 
Dart :: flutter var type 
Dart :: flutter multipline textfield height 
Dart :: How to make checkbox shape to circular using flutter 
Dart :: flutter datatypes check 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =