Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

nqueen problem with python

#Number of queens
print ("Enter the number of queens")
N = int(input())

#chessboard
#NxN matrix with all elements 0
board = [[0]*N for _ in range(N)]

def is_attack(i, j):
    #checking if there is a queen in row or column
    for k in range(0,N):
        if board[i][k]==1 or board[k][j]==1:
            return True
    #checking diagonals
    for k in range(0,N):
        for l in range(0,N):
            if (k+l==i+j) or (k-l==i-j):
                if board[k][l]==1:
                    return True
    return False

def N_queen(n):
    #if n is 0, solution found
    if n==0:
        return True
    for i in range(0,N):
        for j in range(0,N):
            '''checking if we can place a queen here or not
            queen will not be placed if the place is being attacked
            or already occupied'''
            if (not(is_attack(i,j))) and (board[i][j]!=1):
                board[i][j] = 1
                #recursion
                #wether we can put the next queen with this arrangment or not
                if N_queen(n-1)==True:
                    return True
                board[i][j] = 0

    return False

N_queen(N)
for i in board:
    print (i)
Comment

PREVIOUS NEXT
Code Example
Java :: how to pass a float between activities in android studio 
Java :: sort list of list java 
Java :: neither bindingresult nor plain target object for bean name spring mvc 
Java :: bukkit console filter 
Java :: org.apache.hadoop.mapred.JobClient.runJob(JobClient.java:876) 
Java :: how to convert character into operator 
Java :: linked list vs array list vs vector 
Java :: Number formatting java with locale 
Java :: java compare lists 
Java :: Exception in thread "main" java.lang.NoClassDefFoundError sdkmanager 
Java :: does not have a NavController set on 2131231008 
Java :: java stream sort Collator 
Java :: how to run few methods of class after mockStatic 
Java :: make quotation a string 
Java :: mod 10e9+7 in java 
Java :: draw triangle in android studio xml 
Java :: java selection sort 
Java :: vue input pre initial value 
Java :: java program to convert milliseconds to minutes and seconds 
Java :: expression régulière téléphone java 
Java :: How to efficiently find the diameter of a binary tree, in Java? 
Java :: java enum to string 
Java :: copy text from header tag in javacript 
Java :: search 
Java :: print statement in java, simplest java program, Hello World in Java 
Java :: spring boot example with swagger 
Java :: Java long Keyword 
Java :: tostring java 
Java :: edittext editable false android java 
Java :: get string size on screen 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =