Karteikarten
N - Queens
Karten in diesem Stapel (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]
Informationen
- Beschreibung
- Karteikarten am N - Queens, erstellt von Suhas S am 26/05/2018.
- Erstellt von
- Suhas S
- Aufrufe
- 0
- Bewertung