看看Python方法如何快速解决数独游戏
数独是一款有趣又益智的游戏,人脑总是没有程序快,那来就来看看Python方法如何快速解决数独游戏的吧。
#! /usr/bin/env python # -*- coding: utf-8 -*- ''' 解数独 ''' #import sys,os def scan(temp,i): heng=temp[(i/9*9):(i/9*9+9)] shu=[] for i0 in range(0,73,9): shu.append(temp[i%9+i0]) kuai=[] row=i%9/3*3 line=i/27*3 for i0 in range(0,3): kuai.append(temp[line*9+row+i0]) kuai.append(temp[(line+1)*9+row+i0]) kuai.append(temp[(line+2)*9+row+i0]) hsk=heng+shu+kuai hsk=list(set(hsk)) data=[0,1,2,3,4,5,6,7,8,9] map(data.remove,hsk) return data def myprint(temp): if not temp: print 'None' else: for i in range(0,73,9): print temp[i:i+9]
#www.iplaypy.comdef easysolve(test): print 'try easy way...' changeflag=1 while changeflag: changeflag=0 for i in range(0,81): if not test[i]: numleft=scan(test,i) if not numleft: return 0 elif len(numleft)==1: test[i]=numleft[0] changeflag=1 myprint(test) return test def hardsolve(test): print 'try hard way...' i=0 while i<81 and test[i]!=0: i=i+1 if test[i]==0: numleft=scan(test,i) print i+1,':',numleft if not numleft: return 0 elif len(numleft)==1: test[i]=numleft[0] temp=mysolve(test) if not temp: return 0 else: test=temp else: for num in numleft: temp=test[:] temp[i]=num temp=mysolve(temp) if not temp: continue return test def mysolve(test): global result try: result; except: result=[] test=easysolve(test) if not test: print 'wrong' return 0 elif min(test)!=0: result.append(test) return test else: test=hardsolve(test) return test def shudu(test): global result result=[] mysolve(test) return result result=[] test=[\ 0,2,8,0,1,4,0,0,0,\ 0,0,7,0,8,5,0,0,6,\ 1,0,0,7,0,0,0,0,0,\ 0,0,9,8,0,0,0,4,5,\ 0,0,3,5,0,1,9,0,0,\ 5,8,0,0,0,9,1,0,0,\ 0,0,0,0,0,8,0,0,4,\ 9,0,0,3,5,0,8,0,0,\ 0,0,0,4,9,0,5,3,0] temp=shudu(test) print 'Done\n' if temp: i=1 for one in temp: print 'result:',i i=i+1 myprint(one)
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/code/game/g2497.html
相关文章 Recommend
- • 2019年3月最新消息: Python 3.4.10 现已推出
- • [上海]招Python量化系统开发工程师
- • 优集品网络科技有限公司招Python中/高级工程师
- • 爱因互动科技发展有限公司招募Python开发攻城狮
- • mozio招聘Python/Django工程师
- • Kavout金融科技公司招Python研发工程师
- • Python数组逆向输出,编程练习题实例四十
- • Python数组插入排序,编程练习题实例三十九
- • Python矩阵for循环应用,编程练习题实例三十八
- • Python操作Redis数据库方面的问题
- • 请python高手帮我看看这段python代码中函数setter的
- • Python什么方法可以快速将两个队列变成字典
我要分享到:
必知PYTHON教程 Must Know PYTHON Tutorials
- • python 解释器
- • python idle
- • python dir函数
- • python 数据类型
- • python type函数
- • python 字符串
- • python 整型数字
- • python 列表
- • python 元组
- • python 字典
- • python 集合
- • python 变量
- • python print
- • python 函数
- • python 类定义
- • python import
- • python help
- • python open
- • python 异常处理
- • python 注释
- • python continue
- • python pass
- • python return
- • python global
- • python assert
- • python if语句
- • python break
- • python for循环
- • python while循环
- • python else/elif
- • lambda匿名函数
必知PYTHON模块 Must Know PYTHON Modules
- • os 模块
- • sys 模块
- • re 正则表达式
- • time 日期时间
- • pickle 持久化
- • random 随机
- • csv 模块
- • logging 日志
- • socket网络通信
- • json模块
- • urlparse 解析URL
- • urllib 模块
- • urllib2 模块
- • robotparser 解析
- • Cookie 模块
- • smtplib 邮件
- • Base64 编码
- • xmlrpclib客户端
- • string 文本
- • Queue 线程安全
- • math数学计算
- • linecache缓存
- • threading多线程
- • sqlite3数据库
- • gzip压缩解压
最新内容 NEWS
- • pyhton麻将胡牌听牌判断
- • 聊时编写的一个python小游戏 需要pygame模块
- • 下载游戏IP座标等信息的python方法
- • 基于web.py框架带提示的猜数字游戏
- • 益智盒子问题python的解决方法
- • 模拟经营的python小游戏
- • Python编写的点灯小游戏代码
- • python猜数字游戏快速求解解决方案
- • 用Python语言打印杨辉三角形方法示例
- • Python方法生成华容道所有开局
图文精华 RECOMMEND
-
益智盒子问题python的解决方法
-
模拟经营的python小游戏
-
用Python语言打印杨辉三角形方法示
-
Python方法生成华容道所有开局
-
Python编写的俄罗斯方块游戏案例源
-
Python实现自动扫雷游戏的方法源码
热点文章 HOT
- Python实现自动扫雷游戏的方法源码
- 模拟经营的python小游戏
- 用Python语言打印杨辉三角形方法示例
- Python方法生成华容道所有开局
- Python 井字棋三连棋游戏代码
- Python方法解扑克牌24点游戏代码
- 看看python是如何解决三赌徒问题的
- 下载游戏IP座标等信息的python方法