惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

博客园 - fenix

IISExpress 开放局域网访问 sql窗口函数 Sql分页语句 常用SQL 数据库导出结构的sql WebAPI post 跨域调用及坑 单进程运行 MahApps.Metro样式 iis7 添加Mime sudoku Geometric paths,mini language Accessing WMF metadata with C# 标点符号 摘抄 temp C# deep copy 简单对称加密 ICommand ASP.NET 4 Child Applications Fail to Start When Under ASP.NET 2.0 or ASP.NET 3.5 Applications
数独
fenix · 2012-07-04 · via 博客园 - fenix

class SudokuCal:
    def __init__(self):
        self.str1 = '0' * 81
        self.str2 = '0' * 81
        self.answer = 0
        self.done = False
        self.loop = 0
    def same_row(self,i,j):
        return (i%9==j%9)
    def same_col(self,i,j):
        return (i-j)%9==0
    def same_block(self,i,j):
        return (i%27%9/3==j%27%9/3)
    def print_format(self,a):
        for i in range(9):
            for j in range(9):
                print '%s' % a[9*i+j]
            print ''

    def r(self,a):
        self.loop += 1
        if(self.answer==1 and self.loop>100000):
            self.done = True
        i = a.find('0')
        if(i==-1):
            self.print_format(a)
            print ''
            self.answer +=1
            if(self.answer==1):
                self.str1=a
                print('loop %d' % self.loop)
            elif(self.answer==2):
                self.str2=a
                self.done=True
                return
        excluded_numbers = set()
        for j in range(81):
            if(self.same_row(i,j) or self.same_col(i,j) or self.same_block(i,j)):
                excluded_numbers.add(a[j])
        for m in '123456789':
            if(m not in excluded_numbers):
                if(self.done):
                    return
                self.r(a[:i] + m + a[i+1:])

a = [8,0,0,0,0,0,0,0,0,
     0,0,3,6,0,0,0,0,0,
     0,7,0,0,9,0,2,0,0,
     0,5,0,0,0,7,0,0,0,
     0,0,0,0,4,5,7,0,0,
     0,0,0,1,0,0,0,3,0,
     0,0,1,0,0,0,0,6,8,
     0,0,8,5,0,0,0,1,0,
     0,9,0,0,0,0,4,0,0]
b = ''.join([str(i) for i in a ])
s = SudokuCal()
s.r(b)
raw_input()