-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsol.py
63 lines (52 loc) · 2.05 KB
/
sol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from typing import List, Optional
class Solution:
def totalNQueens(self, n: int) -> int:
final = {}
covered = {
"rows": {},
"cols": {},
"negdig": {},
"posdig": {},
}
def isInCoveredLocation(row, col):
if row in covered["rows"] and covered["rows"][row] == True:
return True
if col in covered["cols"] and covered["cols"][col] == True:
return True
difneg = row - col
difpos = row + col
if difneg in covered["negdig"] and covered["negdig"][difneg] == True:
return True
if difpos in covered["posdig"] and covered["posdig"][difpos] == True:
return True
return False
def createKeyFromPlaceQueens(arr):
return "_".join(map(lambda x: str(x), arr))
def backTrack(placedQueens):
if len(placedQueens) == n:
key = createKeyFromPlaceQueens(placedQueens)
final[key] = True
return
row = len(placedQueens)
for cur_col in range(n):
if isInCoveredLocation(row, cur_col):
continue
placedQueens.append(cur_col)
difneg = row - cur_col
difpos = row + cur_col
covered["rows"][row] = True
covered["cols"][cur_col] = True
covered["negdig"][difneg] = True
covered["posdig"][difpos] = True
backTrack(placedQueens)
covered["rows"][row] = False
covered["cols"][cur_col] = False
covered["negdig"][difneg] = False
covered["posdig"][difpos] = False
placedQueens.pop()
backTrack([])
return len(final)
def buildQueenRepresentation(self, arr):
def queenPosToStr(pos):
return "." * pos + "Q" + "." * (len(arr) - pos - 1)
return list(map(queenPosToStr, arr))