LEETCODE —— Sudoku Solver
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的更多相关文章
- [LeetCode] Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- Leetcode: Sudoku Solver
July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...
- [LeetCode] Sudoku Solver(迭代)
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- leetcode—sudoku solver
1.题目描述 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicate ...
- leetcode Sudoku Solver python
#the define of Sudoku is on this link : http://sudoku.com.au/TheRules.aspx Write a program to solve ...
- [LeetCode] Sudoku Solver 解数独,递归,回溯
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- Leetcode之回溯法专题-37. 解数独(Sudoku Solver)
Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...
随机推荐
- 利用QJSON将FDQuery转成JSON串
服务器要支持Http协议,打算采用Http+JSON的方式来交换数据.一开始考虑使用superobject,因为以前使用比较多,比较熟悉. 代码如下: class function FDQueryTo ...
- IOS导航栏颜色渐变与常用属性
(转:http://www.cnblogs.com/Lingchen-start/archive/2015/10/23/4904361.html) 今年很忙,忙的写日志的时间都很少. 少的可怜. 自 ...
- CSS巧妙实现分隔线的几种方法
单个标签实现分隔线: 点此查看实例展示 .demo_line_01{ padding: 0 20px 0; margin: 20px 0; line-height: 1px; border-left: ...
- wchar_t内置还是别名?小问题一则(升级公司以前代码遇到的问题)
问题: 原来的2008工程用2010编译后,运行程序出现无法定位程序输入点 *@basic_string@_WU@*和*@basic_string@G@* 解决: 关闭“语言选项”中“将WChar_t ...
- MVC与webservice上传文件(图片和视频),希望帮且到一些朋友
最近做一个项目,要把图片和视频传到服务器上(网站与图片服务器分开),在网上找了好久,没找到完整的资料. 自己也折腾了半天,才把完整的代码实现完.可能好多朋友都有实现过,没分享代码吧,写得不好希望不要见 ...
- 初学Spring
Spring是当今最流行的框架,今天开始,dayday同学要正式开始学习Spring了,加油 以下是一个简单的应用Spring框架的java程序 src\dayday\HelloSpring.java ...
- c++程序员必知的几个库
c++程序员必知的几个库 1.C++各大有名库的介绍——C++标准库 2.C++各大有名库的介绍——准标准库Boost 3.C++各大有名库的介绍——GUI 4.C++各大有名库的介绍——网络通信 5 ...
- iOS中block的用法 以及和函数用法的区别
ios中block的用法和函数的用法大致相同 但是block的用法的灵活性更高: 不带参数的block: void ^(MyBlock)() = ^{}; 调用的时候 MyBlock(); 带参数的 ...
- HTML5 LocalStorage 本地存储
HTML5 LocalStorage 本地存储 说到本地存储,这玩意真是历尽千辛万苦才走到HTML5这一步,之前的历史大概如下图所示: 最早的Cookies自然是大家都知道,问题主要就是太小,大概也就 ...
- 华为交换机netstream配置
1.配置交换机的流发送 [系统视图]ip netstream timeout active 100 流活跃时间 [系统视图]ip netstream timeout inactive ...