# -*- 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的更多相关文章

  1. [Leetcode][Python]45: Jump Game II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...

  2. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

  3. LeetCode(52) N-Queens II

    题目 Follow up for N-Queens problem. Now, instead outputting board configurations, return the total nu ...

  4. Leetcode之回溯法专题-52. N皇后 II(N-Queens II)

    Leetcode之回溯法专题-52. N皇后 II(N-Queens II) 与51题的代码80%一样,只不过52要求解的数量,51求具体解,点击进入51 class Solution { int a ...

  5. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  6. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  7. 【LeetCode】227. Basic Calculator II 解题报告(Python)

    [LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  8. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  9. Java实现 LeetCode 52 N皇后 II

    52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...

随机推荐

  1. form-validation-engine中的正则表达式

    form-validation-engine是一个不错的表单验证,可以玩玩. (function($) { $.fn.validationEngineLanguage = function() {}; ...

  2. 【转】arm交叉编译器gnueabi、none-eabi、arm-eabi、gnueabihf、gnueabi区别

    原文网址:http://www.veryarm.com/296.html 命名规则 交叉编译工具链的命名规则为:arch [-vendor] [-os] [-(gnu)eabi] arch - 体系架 ...

  3. House Robber 解答

    Question You are a professional robber planning to rob houses along a street. Each house has a certa ...

  4. wireshark----教你如何抓包

    wireshark----教你如何抓包 wireshark是一款强大的抓包工具,走过路过一定不要错过就是了,当你学习TCP/IP协议的时候,学习使用wireshark 抓包正是理论联系实际最好的方法, ...

  5. Java Service Wrapper

    Java Service Wrapper 将Java 应用程序部署成Windows系统服务Java Service Wrapper 1 Product Overview 1 Editions 2 Me ...

  6. 克隆虚拟机,如何将克隆虚拟的网卡设置为eth0

    1.先删掉/etc/udev/rules.d/70-persistent-net.rules文件里的eth0,并要记住eth1的那个网卡的mac地址等下要用.2.把那个eth1修改为eth03.编辑网 ...

  7. git使用经验

    一直想写一点关于git的文章,但是平时太懒了,没有写,现在写些经验这里,方便以后自己忘记了.

  8. Cloudera Manager Free Edition 4.1 和CDH 4.1.2 简易安装教学

    转载:http://fenriswolf.me/2012/12/06/cloudera-manager-free-edition-4-1-和-cdh-4-1-2-简易安装教学/ 安装及管理一个大的Ha ...

  9. ajax 分页完全代码整理

    /* ajax分页 */ var page_cur = 1; //当前页 var total_num, page_size, page_total_num;//总记录数,每页条数,总页数 functi ...

  10. c#datagrid的每行的单击事件

    需要一个帮助类 using System; using System.Net; using System.Windows; using System.Windows.Controls; using S ...