码迷,mamicode.com
首页 > 编程语言 > 详细

[Leetcode][Python]52: N-Queens II

时间:2015-03-27 23:52:53      阅读:409      评论:0      收藏:0      [点我收藏+]

标签:

# -*- coding: utf8 -*-
‘‘‘
__author__ = ‘dabay.wang@gmail.com‘

52: N-Queens II
https://oj.leetcode.com/problems/n-queens-ii/

Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.

===Comments by Dabay===
不知道和N Queen相比有没有简单很多的方法。我这里的解法思路和N Queen一样的。

一个一个放皇后,知道能放下最后一个皇后,解法+1。
放第k个皇后的时候,在第k行中找位置,先看列被占用没有,然后往左上和右上看斜线被占用没有。
‘‘‘

class Solution:
# @return an integer
def totalNQueens(self, n):
def check_up(r, c, board):
for row in xrange(r):
if board[row][c] == ‘Q‘:
return False
else:
return True

def check_upleft(r, c, board):
row = r - 1
column = c - 1
while row>=0 and column>=0:
if board[row][column] == ‘Q‘:
return False
row = row - 1
column = column - 1
else:
return True

def check_upright(r, c, board):
row = r - 1
column = c + 1
while row>=0 and column<len(board):
if board[row][column] == ‘Q‘:
return False
row = row - 1
column = column + 1
else:
return True

def DFS(board, queens, res):
if queens == 0:
res[0] = res[0] + 1
return
r = len(board) - queens
for c in xrange(len(board)):
if not check_up(r, c, board) or not check_upleft(r, c, board) or not check_upright(r, c, board):
continue
else:
board[r][c] = ‘Q‘
DFS(board, queens-1, res)
board[r][c] = ‘.‘

board = [[‘.‘] * n for _ in xrange(n)]
#print board
queens = n
res = [0]
DFS(board, queens, res)
return res[0]


def main():
sol = Solution()
print sol.totalNQueens(5)


if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]52: N-Queens II

标签:

原文地址:http://www.cnblogs.com/Dabay/p/4373035.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!