Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells are indicated by the character '.'.

You may assume that there will be only one unique solution.

A sudoku puzzle...

...and its solution numbers marked in red.

 class Solution(object):
def validset(self ):
s=''
return set(s) def initTbl(self, tbl, board):
for i in range(9):
for j in range(9):
if board[i][j] == '.':
tbl[(i,j)]=[] def solveSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
resultTbl={}
visited=[]
self.initTbl(resultTbl, board)
reuse=False
unvisited = resultTbl.keys()
unvisited.sort()
unvisited.reverse() while unvisited != []:
(x, y) = unvisited.pop()
if reuse==False:
resultTbl[(x,y)] = self.possibleValues((x,y), board)
if resultTbl[(x,y)] == None: # invalid sudoku
print False
break if len( resultTbl[(x,y)] ) == 0: # DEAD END, BACKTRACK
unvisited.append((x, y))
if visited != []:
reuse=True
prev=visited.pop()
unvisited.append(prev)
x=prev[0]
y=prev[1]
board[x][y]='.'
else:
break
continue
board[x][y]=resultTbl[(x,y)].pop()
visited.append((x,y))
reuse=False
for line in board:
if '.' in line:
print False def possibleValues(self, coord, board):
vals = {'.':0}
for i in range(1,10): #init
vals[str(i)]=0 for y in range(0,9):
node=board[coord[0]][y]
vals[node]+=1
if vals[node]>1 and node!='.': return None
for x in range(0,9):
node=board[x][coord[1]]
vals[node]+=1
if vals[node] > 2 and node!='.': return None
x = coord[0]/3*3
y = coord[1]/3*3
for i in range(x, x+3):
for j in range(y, y+3):
node=board[i][j]
vals[node]+=1
if vals[node]>3 and node!='.': return None
s = set()
for k in vals.keys():
if vals[k]>=1: s.add(k)
return self.validset() - s

LEETCODE —— Sudoku Solver的更多相关文章

  1. [LeetCode] Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  2. Leetcode: Sudoku Solver

    July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...

  3. [LeetCode] Sudoku Solver(迭代)

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  4. leetcode—sudoku solver

    1.题目描述 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicate ...

  5. leetcode Sudoku Solver python

    #the define of Sudoku is on this link : http://sudoku.com.au/TheRules.aspx Write a program to solve ...

  6. [LeetCode] Sudoku Solver 解数独,递归,回溯

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  7. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  8. [Leetcode][Python]37: Sudoku Solver

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...

  9. Leetcode之回溯法专题-37. 解数独(Sudoku Solver)

    Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...

随机推荐

  1. c语言实现牛顿迭代法

    #include<stdio.h> #include<stdlib.h> #include<math.h> #include<float.h> #inc ...

  2. USB协议规范学习(一)

    什么是USB OHCI规范? OHCI(Open HCI)是目前使用比较广泛的三种USB主机控制器规范之一.USB体系结构是由四个主要部分组成:客户软件/USB驱动,主机控制器驱动(HCD),主机控制 ...

  3. VMware下利用ubuntu13.04建立嵌入式开发环境之四

    二.telnet.SSH服务器安装与配置 1.telnet 1.1 安装服务器:apt-get install xinetd telnetd 1.2 安装openbsd-inetd:apt-get i ...

  4. 重读高程3: c2-3 script元素

    1. 异步脚本一定会在页面的load事件前执行,但可能会在domC ontentLoaded事件触发之前或者之后执行.

  5. MongooseJS 4.6.4 发布,MongoDB 连接包

    MongooseJS 4.6.4  发布了,MongooseJS 是基于 node.js,使用 JavaScript 编程,连接 MongoDB 数据库的软件包,使MongoDB 的文档数据模型变得优 ...

  6. snort installation, configuration and test

    snort installation: https://www.snort.org/#get-started wget https://www.snort.org/rules/snortrules-s ...

  7. C#事件支持发布者/订阅者模式(观察者模式)

    C#事件支持发布者/订阅者模式,发布者将事件通知给订阅者,而订阅者在事件发生时调用已经注册好的事件处理函数.        public delegate void delUpdate();  //委 ...

  8. 活用shape、selector和layer-list来打造自己想要的背景效果

    活用shape.selector和layer-list来打造自己想要的背景效果 2016-04-27 13:52 281人阅读 评论(0) 收藏 举报 版权声明:本文为博主原创文章,未经博主允许不得转 ...

  9. css2----兼容----ie67的3像素bug

    发生条件:当浮动元素和非浮动元素相邻 时候,ie67下,两个元素就会多出3像素的间隔,其实是浮动元素产生的margin值 解决办法:1:让没有浮动的元素也浮动: 2:让浮动元素产生margin-*:- ...

  10. SQL中随机数函数rand()简介

    转自:http://database.51cto.com/art/201009/224397.htm 下文将为您介绍SQL中的随机函数rand(),供您参考,如果您是才接触SQL Server的新手, ...