GoConqr has a new look! Let us know what you think.

Flashcards

N - Queens

Suhas S English 1 card

Cards in this deck (1)

  1. 1Input: 4 Output: [ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."] ] Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.class Solution(object):
    def solveNQueens(self, n):
    def DFS(queens, xy_dif, xy_sum):
    p = len(queens)
    if p==n:
    result.append(queens)
    return None
    for q in range(n):
    if q not in queens and p-q not in xy_dif and p+q not in xy_sum:
    DFS(queens+[q], xy_dif+[p-q], xy_sum+[p+q])
    print(xy_dif)
    print(xy_sum)
    # print( queens )
    result = []
    DFS([],[],[])
    print(result)
    return [ ["."*i + "Q" + "."*(n-i-1) for i in sol] for sol in result]

Information

Description
Flashcards on N - Queens, created by Suhas S on 26/05/2018.
Created by
Suhas S
Views
0
Rating

Feedback

What is it about?

What do you think of the new look?

How do you like it?