GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/python/051_N-Queens.py at master · lxy1320/leetcode Expand file tree/ Copy path051_N-Queens.pyMore file actions 42 lines (39 loc) · 1.15 KB / Copy path051_N-Queens.pyFile metadata and controls42 lines (39 loc) · 1.15 KB 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 class Solution(object): def solveNQueens(self, n): """ :type n: int :rtype: List[List[str]] """ # recusive if n == 0: return 0 res = [] board = [['.'] * n for t in range(n)] self.do_solveNQueens(res, board, n) return res def do_solveNQueens(self, res, board, num): if num == 0: res.append([''.join(t) for t in board]) return ls = len(board) pos = ls - num check = [True] * ls for i in range(pos): for j in range(ls): if board[i][j] == 'Q': check[j] = False step = pos - i if j + step < ls: check[j + step] = False if j - step >= 0: check[j - step] = False break for j in range(ls): if check[j]: board[pos][j] = 'Q' self.do_solveNQueens(res, board, num - 1) board[pos][j] = '.' if __name__ == '__main__': # begin s = Solution() print s.solveNQueens(4) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |