python第六周拼图_python – 解决n-queen拼图
我刚刚解决了
python中的nqueen问题.该解决方案输出了将n个皇后放置在nXn棋盘上的解决方案总数,但尝试使用n = 15需要一个多小时才能得到答案.任何人都可以看看代码,并给我提示加快这个程序……一个新手python程序员.
#!/usr/bin/env python2.7
##############################################################################
# a script to solve the n queen problem in which n queens are to be placed on
# an nxn chess board in a way that none of the n queens is in check by any other
#queen using backtracking'''
##############################################################################
import sys
import time
import array
solution_count = 0
def queen(current_row, num_row, solution_list):
if current_row == num_row:
global solution_count
solution_count = solution_count + 1
else:
current_row += 1
next_moves = gen_nextpos(current_row, solution_list, num_row + 1)
if next_moves:
for move in next_moves:
'''make a move on first legal move of next moves'''
solution_list[current_row] = move
queen(current_row, num_row, solution_list)
'''undo move made'''
solution_list[current_row] = 0
else:
return None
def gen_nextpos(a_row, solution_list, arr_size):
'''function that takes a chess row number, a list of partially completed
placements and the number of rows of the chessboard. It returns a list of
columns in the row which are not under attack from any previously placed
queen.
'''
cand_moves = []
'''check for each column of a_row which is not in check from a previously
placed queen'''
for column in range(1, arr_size):
under_attack = False
for row in range(1, a_row):
'''
solution_list holds the column index for each row of which a
queen has been placed and using the fact that the slope of
diagonals to which a previously placed queen can get to is 1 and
that the vertical positions to which a queen can get to have same
column index, a position is checked for any threating queen
'''
if (abs(a_row - row) == abs(column - solution_list[row])
or solution_list[row] == column):
under_attack = True
break
if not under_attack:
cand_moves.append(column)
return cand_moves
def main():
'''
main is the application which sets up the program for running. It takes an
integer input,N, from the user representing the size of the chessboard and
passes as input,0, N representing the chess board size and a solution list to
hold solutions as they are created.It outputs the number of ways N queens
can be placed on a board of size NxN.
'''
#board_size = [int(x) for x in sys.stdin.readline().split()]
board_size = [15]
board_size = board_size[0]
solution_list = array.array('i', [0]* (board_size + 1))
#solution_list = [0]* (board_size + 1)
queen(0, board_size, solution_list)
print(solution_count)
if __name__ == '__main__':
start_time = time.time()
main()
print(time.time()
python第六周拼图_python – 解决n-queen拼图相关推荐
- Python第六周作业
Python第六周作业 1. 正则表达式的点星匹配 2. 计算函数曲线与x轴包围的面积 3. 哥德巴赫猜想 4. 鸡兔同笼B 5. 与7无关的数 6. 完美立方数 7. 高次方程求根 8. 在终端输出 ...
- 慕课python第六周测验答案_中国大学慕课答案第六章节答案_Python语言程序设计作业答案...
中国大学慕课答案第六章节答案_Python语言程序设计作业答案 更多相关问题 求证:tan(x+y)+tan(x-y)=sin2xcos2x-sin2y. 若cosθ1+tan2θ+sinθ1+cot ...
- python第六周实验_机器学习 | 吴恩达机器学习第六周编程作业(Python版)
实验指导书 下载密码:ovyt 本篇博客主要讲解,吴恩达机器学习第六周的编程作业,作业内容主要是实现一个正则化的线性回归算法,涉及本周讲的模型选择问题,绘制学习曲线判断高偏差/高方差问题.原始实 ...
- python idea控制台中文乱码_python 解决cv2绘制中文乱码问题
python 解决cv2绘制中文乱码问题 因为使用cv2.putText() 只能显示英文字符,中文会出现乱码问题, 因此使用PIL在图片上绘制添加中文,可以指定字体文件. 大体思路: OpenCV图 ...
- 慕课python第六周测验答案_中国大学慕课答案第八单元测试答案_Python语言程序设计考试测验答案...
已知总人口为195.4万人,成年人口为139.7万人,失业者数量为5.7万人,就业者数量为92.3万人,失业率等于( ). 某投资项目,期初投资500万元,1年后回收资金750万元,第2年初投入250 ...
- 慕课python第六周测验答案_中国大学mooc慕课_Python程序设计_章节测验答案
摘要: 而非据是的主要证来客非洲,中国章节目前,比较人祖门齿先的中国化石.热灭灭菌灭菌灭菌选择下列线灭注射最好紫外干热菌法菌法哪种用油微波方法法B法.慕课湿.不包执行踪结踪D果C及跟件原情况内容意见因 ...
- python判断今天周几_Python如何根据日期判断周几
python作为现在很流行的一门语言,学好python是很有必要的,下面是根据日期判断周几的个人总结的一些方法,可供参考. Python如何根据日期判断周几 Python判断周几主要使用了time, ...
- python第三周笔记_Python第四周 学习笔记(1)
函数 Python的函数没有return语句,隐式会返回一个None值 函数是可调用的对象,callable() 函数参数 参数调用时传入的参数要和定义的个数相匹配(可变参数例外) 位置参数 def ...
- python单行动态刷新不了_python 解决tqdm模块不能单行显示的问题
python 解决tqdm模块不能单行显示的问题 OS: Windows 10 IDE: Anaconda Spyder (Python3.6) 代码如下: from tqdm import tqdm ...
- python汉字的unicode编码_python解决汉字编码问题:Unicode Decode Error
前言 最近由于项目需要,需要读取一个含有中文的txt文档,完了还要保存文件.文档之前是由base64编码,导致所有汉字读取显示乱码.项目组把base64废弃之后,先后出现两个错误: ascii cod ...
最新文章
- c++类的构造函数详解
- 机房墙面为什么要做保温?该怎么做?
- java位移运算符2 转
- Java秒杀系统实战系列~基于Redis的原子操作优化秒杀逻辑
- 阿里云峰会|阿里云数据中台重磅升级后拟扶持100万家企业数智化
- 技术差的程序员,90%都输在这点上!骨灰级开发:其实都是在瞎努力!
- JavaScript高级程序设计笔记 - 第四章 变量 作用域 内存问题
- IDEA 2020 配置 Maven 创建 Spring Boot 项目
- Java变量的默认值和初始化
- 几台服务器做虚拟化,4台服务器虚拟化教程(多台服务器虚拟成一台)
- DarkGDK的杯具体验
- matlab怎么求过渡矩阵,求过渡矩阵的方法
- 投资20亿元,又一个云手机基地诞生
- 小学身高体重测试软件,儿童身高体重在线测评
- java tld tag_jsp中引用的tag和tld文件
- neo4j :rel_Neo4j:足球转移图表
- Java 确定线程池中工作线程数的大小
- 从零开始,创建一个VUE项目,详细图文详解。
- 理解目标检测中的老大难:小目标检测
- DockerCon 2016 西雅图即将到来,8大主题详尽解读
热门文章
- Invalid bound statement (not found)出现的原因和解决方法
- perl php-serialization install,如何在PHP中反序列化Perl Data :: Dumper输出
- C语言程序设计第四次作业-选择结构(2)
- [Err] 1062 - Duplicate entry '111' for key 'PRIMARY'
- Android手机截图怎么做,手机截屏怎么弄,详细教您手机截图方法
- gis热力专题图制作
- Python网页截图/屏幕截图/截长图如何实现?
- python简单操作excel
- 【Leetcode刷题Python】1467. 两个盒子中球的颜色数相同的概率
- java-数组_length 字符串_length() 集合_size()的区别