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 ...
随机推荐
- java开发模式学习
1.瀑布模式 这种模式适合小项目,一层层进行编码,没有规模的设计, 2.原型模式 先做模板给客户在做实体 3.面向对象模式 用面向对象的思想进行开发 4.螺旋模式 从内到外一层层开,
- job1
http://www.newsmth.net/nForum/#!article/Career_Upgrade/221039 http://www.newsmth.net/nForum/#!articl ...
- Maven基础知识(转)
文章摘自http://www.cnblogs.com/xing901022/p/4170248.html 谢谢楼主的总结,界面设计的很好看! 一.什么是Maven Maven是一个用于项目构建的工具, ...
- system_call中断处理过程分析
本文所有的分析内容都是基于Linux3.18.6内核,鉴于对应不同内核版本,系统调用的实现不相同.若需要分析其他版本内核的系统调用的实现过程,请谨慎参考. system_call函数的功能是用来响应外 ...
- codeforces 651C(map、去重)
题目链接:http://codeforces.com/contest/651/problem/C 思路:结果就是计算同一横坐标.纵坐标上有多少点,再减去可能重复的数量(用map,pair存一下就OK了 ...
- LayaAir引擎——(三)
LyaAir引擎(JavaScript)实现图片的翻转一半 图片4.png位于bin/开场过渡 文件夹下,图片大小150*30(根据实际情况做调整) var button; var scale1 = ...
- libc++
今天测试最新的微信iOS SDK, 仅仅是建了一个空的工程,把sdk加进去运行,就报了以下错误: Undefined symbols for architecture x86_64: "op ...
- 第二章 搭建Android开发环境--读书笔记
俗话说,工欲善其事,必先利其器,对于Android驱动开发来说,首先我们要做的就是搭建Android开发环境,我们首先要配置Linux驱动的开发环境,接着还得配置开发Android应用程序以及Andr ...
- java 读写properties
网速不好:先贴上资料: Java配置文件Properties的读取.写入与更新操作 [Spring] - Property注入 http://www.360doc.com/content/14/073 ...
- 安卓微信浏览器中location.href失效的问题
在移动web中,经常会使用window.location.href去跳转页面,这个方法在绝大多数浏览器中都不会存在问题,但是在安卓手机的微信自带浏览器中,会出现一个奇怪的bug. window.loc ...