|
| 1 | +# Python program to solve N Queen |
| 2 | +# Problem using backtracking |
| 3 | + |
| 4 | +global N |
| 5 | +N = 4 |
| 6 | + |
| 7 | +def printSolution(board): |
| 8 | + for i in range(N): |
| 9 | + for j in range(N): |
| 10 | + print (board[i][j],end=' ') |
| 11 | + print() |
| 12 | + |
| 13 | + |
| 14 | +# A utility function to check if a queen can |
| 15 | +# be placed on board[row][col]. Note that this |
| 16 | +# function is called when "col" queens are |
| 17 | +# already placed in columns from 0 to col -1. |
| 18 | +# So we need to check only left side for |
| 19 | +# attacking queens |
| 20 | +def isSafe(board, row, col): |
| 21 | + |
| 22 | + # Check this row on left side |
| 23 | + for i in range(col): |
| 24 | + if board[row][i] == 1: |
| 25 | + return False |
| 26 | + |
| 27 | + # Check upper diagonal on left side |
| 28 | + for i, j in zip(range(row, -1, -1), range(col, -1, -1)): |
| 29 | + if board[i][j] == 1: |
| 30 | + return False |
| 31 | + |
| 32 | + # Check lower diagonal on left side |
| 33 | + for i, j in zip(range(row, N, 1), range(col, -1, -1)): |
| 34 | + if board[i][j] == 1: |
| 35 | + return False |
| 36 | + |
| 37 | + return True |
| 38 | + |
| 39 | +def solveNQUtil(board, col): |
| 40 | + # base case: If all queens are placed |
| 41 | + # then return true |
| 42 | + if col >= N: |
| 43 | + return True |
| 44 | + |
| 45 | + # Consider this column and try placing |
| 46 | + # this queen in all rows one by one |
| 47 | + for i in range(N): |
| 48 | + |
| 49 | + if isSafe(board, i, col): |
| 50 | + # Place this queen in board[i][col] |
| 51 | + board[i][col] = 1 |
| 52 | + |
| 53 | + # recur to place rest of the queens |
| 54 | + if solveNQUtil(board, col + 1) == True: |
| 55 | + return True |
| 56 | + |
| 57 | + # If placing queen in board[i][col |
| 58 | + # doesn't lead to a solution, then |
| 59 | + # queen from board[i][col] |
| 60 | + board[i][col] = 0 |
| 61 | + |
| 62 | + # if the queen can not be placed in any row in |
| 63 | + # this column col then return false |
| 64 | + return False |
| 65 | + |
| 66 | +# This function solves the N Queen problem using |
| 67 | +# Backtracking. It mainly uses solveNQUtil() to |
| 68 | +# solve the problem. It returns false if queens |
| 69 | +# cannot be placed, otherwise return true and |
| 70 | +# placement of queens in the form of 1s. |
| 71 | +# note that there may be more than one |
| 72 | +# solutions, this function prints one of the |
| 73 | +# feasible solutions. |
| 74 | +def solveNQ(): |
| 75 | + board = [ [0, 0, 0, 0], |
| 76 | + [0, 0, 0, 0], |
| 77 | + [0, 0, 0, 0], |
| 78 | + [0, 0, 0, 0] |
| 79 | + ] |
| 80 | + |
| 81 | + if solveNQUtil(board, 0) == False: |
| 82 | + print ("Solution does not exist") |
| 83 | + return False |
| 84 | + |
| 85 | + printSolution(board) |
| 86 | + return True |
| 87 | + |
| 88 | +# driver program to test above function |
| 89 | +solveNQ() |
| 90 | + |
| 91 | +# This code is contributed by Divyanshu Mehta |
0 commit comments