# -*- 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. IIS缺少文件的解决方法

    原文 http://cqyccmh.blog.163.com/blog/static/6068134720102211543944/ 今天解决了一个郁闷了很久的问题,之前实在没辙就只能重装系统,因为装 ...

  2. ./configure : /bin/sh^M : bad interpreter

    用命令行来编译Qt的时候发生标题尚的错误. 原因是文件中带有DOS行结束符,必须把它转换成UNix结束符 references: http://stackoverflow.com/questions/ ...

  3. PKU 3667 Hotel(线段树)

    Hotel The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...

  4. 【转】android camera(三):camera V4L2 FIMC

    关键词:android  camera CMM 模组 camera参数  CAMIF   V4L2  平台信息:内核:linux系统:android 平台:S5PV310(samsung exynos ...

  5. 分享几种Linux软件的安装方法

    Linux软件安装由于不同的Linux分支,安装方法也互不相同,介绍几种常见的安装方法. 1. 源码安装,     对于本身具有开源血统的Linux系统来说,几乎所有的开源软件都支持在Linux平台运 ...

  6. IOS ViewController 生命周期

    加载过程 第一步 -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 第二步 -(void) ...

  7. jquery中push()的用法(数组添加元素)

    push定义和用法 push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度. 语法 arrayObject.push(newelement1,newelement2,....,newele ...

  8. Failed to create the Java Virtual Machine (Myeclipse或者eclipse启动报错)

    把某几个值改为原来的0.5倍就ok了(我就这么解决的)   eclipse.ini如下:   -startupplugins/org.eclipse.equinox.launcher_1.2.0.v2 ...

  9. /boot磁盘空间不足,没盘扩展,只好删除旧内核了

    [xiejdm@localhost ~]$ uname -r -.el7.x86_64 [xiejdm@localhost ~]$ sudo rpm -qa | grep kernel kernel- ...

  10. pl sql练习(4)

    1.ROW_NUMBER 返回连续的排位,不论值是否相等 select deptno,ename,sal, row_number () over (partition by deptno order ...