栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

蓝桥杯python:方格填数

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

蓝桥杯python:方格填数

题目:

程序说明:
此题我用了一个耗时较长的程序解决的。 用了一个库from itertools import permutations,返回可迭代对象的所有数学全排列方式,例如
将0到9十个数进行全排列,然后再陆续进行判断连续数字是否相邻

全部代码:

from itertools import permutations

li=[i for i in range(10)]
x=list(permutations(li,10))
count=0
for i in x:
    res=False
    if abs(i[0]-i[4])==1 or abs(i[0]-i[3])==1 or abs(i[0]-i[5])==1 or abs(i[0]-i[1])==1:
        res=True
    if abs(i[1]-i[0])==1 or abs(i[1]-i[2])==1 or abs(i[1]-i[5])==1 or abs(i[1]-i[4])==1 or abs(i[1]-i[6])==1 :
        res=True
    if abs(i[2]-i[1])==1 or abs(i[5]-i[2])==1 or abs(i[2]-i[6])==1 :
        res=True
    if abs(i[3]-i[0])==1 or abs(i[3]-i[4])==1 or abs(i[3]-i[8])==1 or abs(i[3]-i[7])==1 :
        res=True
    if abs(i[4]-i[0])==1 or abs(i[4]-i[3])==1 or abs(i[4]-i[7])==1 or abs(i[8]-i[4])==1 or abs(i[4]-i[9])==1 or abs(i[5]-i[4])==1 or abs(i[4]-i[1])==1:
        res=True
    if abs(i[5]-i[1])==1 or abs(i[5]-i[0])==1 or abs(i[5]-i[4])==1 or abs(i[5]-i[8])==1 or abs(i[5]-i[9])==1 or abs(i[5]-i[6])==1 or abs(i[5]-i[2])==1 :
        res=True
    if abs(i[6]-i[2])==1 or abs(i[6]-i[1])==1 or abs(i[6]-i[5])==1 or abs(i[6]-i[9])==1  :
        res=True
    if abs(i[7]-i[3])==1 or abs(i[7]-i[4])==1 or abs(i[7]-i[8])==1 :
        res=True
    if abs(i[8]-i[3])==1 or abs(i[8]-i[4])==1 or abs(i[8]-i[5])==1 or abs(i[8]-i[7])==1 or abs(i[8]-i[9])==1 :
        res=True
    if abs(i[9]-i[8])==1 or abs(i[9]-i[4])==1 or abs(i[9]-i[5])==1 or abs(i[9]-i[6])==1:
        res=True
    if not flag:
        count+=1
print(count)

>>> 1580
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/740073.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号