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

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
Google DeepMind News
Google DeepMind News
云风的 BLOG
云风的 BLOG
WordPress大学
WordPress大学
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
I
InfoQ
小众软件
小众软件
Recent Announcements
Recent Announcements
博客园 - 【当耐特】
The GitHub Blog
The GitHub Blog
大猫的无限游戏
大猫的无限游戏
美团技术团队
T
The Blog of Author Tim Ferriss
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
MongoDB | Blog
MongoDB | Blog
V
V2EX
J
Java Code Geeks
有赞技术团队
有赞技术团队
博客园 - 聂微东
B
Blog RSS Feed
博客园 - 司徒正美

博客园 - 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()