原题地址:https://oj.leetcode.com/problems/n-queens-ii/

题意:和N-Queens这道题其实是一样的,只不过这次要求返回的时N皇后的解的个数的问题。

解题思路:上道题使用了递归回溯的解法,这道题我们可以使用非递归回溯来解决,因为如果使用递归回溯来解决,那么代码和上道题几乎一样。在非递归的编程中,比较有技巧性的是如何来进行回溯。

代码:

class Solution:
# @return an integer
def totalNQueens(self, n):
def check(k, j): # check if the kth queen can be put in column j!
for i in range(k):
if board[i]==j or abs(k-i)==abs(board[i]-j):
return False
return True
board=[-1 for i in range(n)]
row=0; col=0; sum=0
while row<n:
while col<n:
if check(row, col):
board[row]=col
col=0
break
else:
col+=1
if board[row]==-1:                  #如果为真,即为在这一行找不到位置放置皇后
if row==0:                    #如果在第0行也找不到位置放置皇后了,说明所有的情况已经迭代完毕了,执行break跳出操作。
break
else:
row-=1 #这条语句用来回溯到上一行
col=board[row]+1              #回溯到上一行时,皇后放置的位置要加1,也就是开始试验下一列
board[row]=-1                #然后将这一行的值重置为-1,也就是说要重新寻找皇后的位置了
continue
if row==n-1:                     #当row==n-1时,说明最后一行的皇后位置也确定了,得到了一个解
sum+=1
col=board[row]+1
board[row]=-1
continue
row+=1
return sum

[leetcode]N-Queens II @ Python的更多相关文章

  1. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  2. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  3. [leetcode]Spiral Matrix II @ Python

    原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...

  4. [leetcode]Word Break II @ Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

  5. [leetcode]Palindrome Partitioning II @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s  ...

  6. [leetcode]Path Sum II @ Python

    原题地址:https://oj.leetcode.com/problems/path-sum-ii/ 题意: Given a binary tree and a sum, find all root- ...

  7. [leetcode]Single Number II @ Python

    原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...

  8. [Leetcode] n queens ii n皇后问题

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

  9. leetcode Jump Game II python

    @link http://www.cnblogs.com/zuoyuan/p/3781953.htmlGiven an array of non-negative integers, you are ...

  10. leetcode Combination Sum II python

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

随机推荐

  1. SDOI2013 R1 Day2

    目录 2018.3.25 Test 总结 T1 BZOJ.3129.[SDOI2013]方程(扩展Lucas 容斥) T2 洛谷.3305.[SDOI2013]费用流(最大流ISAP 二分) T3 B ...

  2. NOI.AC NOIP模拟赛 第三场 补记

    NOI.AC NOIP模拟赛 第三场 补记 列队 题目大意: 给定一个\(n\times m(n,m\le1000)\)的矩阵,每个格子上有一个数\(w_{i,j}\).保证\(w_{i,j}\)互不 ...

  3. 闲话函数式变成与OOP

    函数式编程扫盲篇 推薦參考文獻地址:http://byvoid.github.io/slides/apio-fp/index.html 1. 概论 在过去的近十年的时间里,面向对象编程大行其道.以至于 ...

  4. hdu 4169 二分匹配最大独立集 ***

    题意:有水平N张牌,竖直M张牌,同一方向的牌不会相交.水平的和垂直的可能会相交,求最少踢出去几张牌使剩下的牌都不相交. 二分匹配 最小点覆盖=最大匹配. 链接:点我 坐标点作为匹配的端点 #inclu ...

  5. Develop with asyncio部分的翻译

    Develop with asyncio 异步程序和普通的连续程序(也就是同步程序)是很不一样的,这里会列出一些常见的陷阱,并介绍如何去避开他们. Debug mode of asyncio 我们用a ...

  6. sTM32 使用TIMx_CH1作为 Tx1F_ED 计数器时钟

    环境:iar arm 5.3 stm32f103vbt6 使用PA.8 外部输入10Mhz的方波.可从systick中断得到数据4. 4×5000(预分频值)×1000(tick中断时间)=20MHz ...

  7. 阿里云esc云服务器IP不能访问的解决办法

    问题:阿里云服务器,专有网络,web设置完毕,在服务器中localhost能够访问,并且关闭防火墙,但是使用公网ip无法访问. 解决:找到本实例安全组,配置规则,按照要求填入80或其他端口.配置完成后 ...

  8. Linux Delay Accounting

    https://andrestc.com/post/linux-delay-accounting/ Ever wondered how long is your program spending wh ...

  9. swap文件查看

    建议 Swap 使用单独的分区: a swap file a combination of swap partitions and swap files. Swap 大小的计算公式: M 等于物理内存 ...

  10. MTK65XX平台充电调试总结

    MTK平台充电调试总结 摘要:调试电池的充放电管理,首先须要深入了解锂电池的电池原理和特点.充放电特性以及主要的电池安全问题.然后须要对MTK的电池管理驱动程序有深入的了解.理解电池充放电算法的基本原 ...