# n queen problem using branch and bound | python3 program to solve n queen problem using branch or bound ; a utility function to pri nt solution ; a optimized function to check if a queen can be placed on board [ row ] [ col ] ; a recursive utility function to solve n queen problem ; base case : if all queens are placed then return true ; consider this column and try placing this queen in all rows one by one ; check if queen can be placed on board [ i ] [ col ] ; place this queen in board [ i ] [ col ] ; recur to place rest of the queens ; remove queen from board [ i ] [ col ] ; if queen can not be place in any row in this colum col then return false ; this function solves the n queen problem using branch or bound . it mainly uses solvenqueensutil ( ) to solve the problem . it returns false if queens cannot be placed , otherwise return true or prints placement of queens in the form of 1 s . please note that there may be more than one solutions , this function prints one of the feasible solutions . ; helper matrices ; arrays to tell us which rows are occupied ; keep two arrays to tell us which diagonals are occupied ; initialize helper matrices ; solution found ; 

N = 8


def printSolution(board):
    for i in range(N):
        for j in range(N):
            print(board[i][j], end=" ")
        print()


def isSafe(
        row,
        col,
        slashCode,
        backslashCode,
        rowLookup,
        slashCodeLookup,
        backslashCodeLookup):
    if (slashCodeLookup[slashCode[row][col]]
            or backslashCodeLookup[backslashCode[row][col]] or rowLookup[row]):
        return False
    return True


def solveNQueensUtil(
        board,
        col,
        slashCode,
        backslashCode,
        rowLookup,
        slashCodeLookup,
        backslashCodeLookup):
    if (col >= N):
        return True
    for i in range(N):
        if (isSafe(i,
                   col,
                   slashCode,
                   backslashCode,
                   rowLookup,
                   slashCodeLookup,
                   backslashCodeLookup)):
            board[i][col] = 1
            rowLookup[i] = True
            slashCodeLookup[slashCode[i][col]] = True
            backslashCodeLookup[backslashCode[i][col]] = True
            if (solveNQueensUtil(board, col + 1, slashCode, backslashCode,
                                 rowLookup, slashCodeLookup, backslashCodeLookup)):
                return True
            board[i][col] = 0
            rowLookup[i] = False
            slashCodeLookup[slashCode[i][col]] = False
            backslashCodeLookup[backslashCode[i][col]] = False
    return False


def solveNQueens():
    board = [[0 for i in range(N)]for j in range(N)]
    slashCode = [[0 for i in range(N)]for j in range(N)]
    backslashCode = [[0 for i in range(N)]for j in range(N)]
    rowLookup = [False] * N
    x = 2 * N - 1
    slashCodeLookup = [False] * x
    backslashCodeLookup = [False] * x
    for rr in range(N):
        for cc in range(N):
            slashCode[rr][cc] = rr + cc
            backslashCode[rr][cc] = rr - cc + 7
    if (solveNQueensUtil(board, 0, slashCode, backslashCode,
                         rowLookup, slashCodeLookup, backslashCodeLookup) == False):
        print("Solution does not exist")
        return False
    printSolution(board)
    return True


solveNQueens()

# Expected output:
1 0 0 0 0 0 0 0 
0 0 0 0 0 0 1 0 
0 0 0 0 1 0 0 0 
0 0 0 0 0 0 0 1 
0 1 0 0 0 0 0 0 
0 0 0 1 0 0 0 0 
0 0 0 0 0 1 0 0 
0 0 1 0 0 0 0 0