N Queen Problem (Data Structures and Algorithms #9)(Recursion #6)(Backtracking #5)

preview_player
Показать описание
This video explains how to solve N Queen Problem by using backtracking in recursion.

Рекомендации по теме
Комментарии
Автор

great work sir. You have cleared my lots of doubt of the backtracking. Please add videos based on the dynamic programming.

rishabhshukla
Автор

Excellent explanation brother all my backtracking doubts are clear now

mallikarjunpidaparthi
Автор

Love it, please keep making more videos! If possible on DP too!

ektadhobley
Автор

Best Resource to understand Backtracking.Please add video fro Sudoku as well.

arunrnambiar
Автор

By far the best video on this problem. Kudos /\

arpitasood
Автор

I replaced "if NQueen(board, size, rowNew)) {return true}" with 'NQueen(board, size, rowNew)' and got all possible solutions :)

russianharrypotter
Автор

Awesome video like others...
Sir need some more problems using recursion to others data structures like Tree, linkedlist etc (:

HIMANSHUSHARMA-ehwv
Автор

Nice video sir,
sir please make video on how to switch career from companies like infosys, wipro, tcs, etc to product based companies.

mayankkandpal
Автор

Python code for the same:

'''
N Queen Problem
'''

def IFvalidCell(board, rowNew, colNew, size):
valid = True
for i in range(size):
if board[i][colNew]:
valid = False
for i, j in zip(range(rowNew, -1, -1), range(colNew, -1, -1)):
if board[i][j]:
valid = False
for i, j in zip(range(rowNew, -1, -1), range(colNew, size)):
if board[i][j]:
valid = False
return valid

def NQueen(board, size, row):
if row == size-1:
for i in range(size):
for j in range(size):
print(board[i][j], end=" ")
print("\n")
return True
else:
for col in range(size):
rowNew = row + 1
if IFvalidCell(board, rowNew, col, size):
board[rowNew][col]=True
if(NQueen(board, size, rowNew)):
return True
board[rowNew][col]=False
return False


if __name__ == "__main__":
board = [[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False]]
NQueen(board, 4, -1)

chitranjanjangid
join shbcf.ru