#the define of Sudoku is on this link : http://sudoku.com.au/TheRules.aspx

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 solveSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
"""
def isValid(x,y):
tmp=board[x][y]
board[x][y]='D'
for i in range(9):
if board[i][y] == tmp:
return False
for i in range(9):
if board[x][i] == tmp:
return False
for i in range(3):
for j in range(3):
if board[(x/3)*3+i][(y/3)*3+j] == tmp:
return False
board[x][y]=tmp
return True
def dfs(board):
for i in range(9):
for j in range(9):
if board[i][j] == '.':
for k in '':
board[i][j] = k
if isValid(i,j) and dfs(board):
return True
board[i][j] = '.'
return False
return True
dfs(board)

leetcode Sudoku Solver python的更多相关文章

  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(迭代)

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

  5. leetcode—sudoku solver

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

  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][Python]37: Sudoku Solver

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

  8. Leetcode 笔记 36 - Sudoku Solver

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

  9. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

随机推荐

  1. SpringMVC+easyui显示数据

    近期做毕业设计,想用easyui,先学习一下CRUD.今天先弄了个表格显示数据库的数据.jsp页面还有非常多其他元素,我就不贴上去了.我显示数据的JSP为/WebContent/WEB-INF/vie ...

  2. <转>凯文·凯利斯坦福演讲-预言未来20年科技潮流

    Note:未来全部的生意都是关于数据的生意,近场通信.自组网介入网络.人工智能...,如今的IT科技界是否仅仅是冰山一角.斑斓舞台帷幕的一丝缝隙? 原文出处: 中欧管理工商学院   欢迎分享原创到伯乐 ...

  3. 从头开始-06.C语言中预处理指令

    预处理指令 不带参数的宏定义: 格式: #define 宏名 值 作用:提高代码的可读性 在程序编译前把所有出现宏名标示的位置都替换为定义宏的时候,宏名后面的值 带参数的宏定义 格式 #define ...

  4. (原) Jquery 判断移动设备是IOS / Android系统

    var u = navigator.userAgent; var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > - ...

  5. DOM常见属性及用法

    1:innerHTML.outerHTML.innerText.outerText innerHTML: 设置或获取位于对象起始和结束标签内的HTML. outerHTML: 设置或获取对象及其内容的 ...

  6. (Qt 翻译) QGLSceneNode

    #include <QGLSceneNode> QGLSceneNode ( QObject * parent = 0 ) QGLSceneNode ( const QGeometryDa ...

  7. 关于ASP .Net Core 引用dll 一

    一:ASP.Net Core 引用dll文件,不可以直接引用,必须在NuGet中引用才行. 二:如果想引用自己的dll文件,则需要注册NeGet账号,获取到API Key 才行,还需要下载NuGet安 ...

  8. Fedora 开启 ssh

    Fedora 17 已经安装好openssh server了 不用再装 不过默认无开启 首先su root1.开启ssh服务# systemctl start sshd.service 2.随系统一起 ...

  9. 查看 并发请求数及其TCP连接状态

    服务器上的一些统计数据: 1)统计80端口连接数netstat -nat|grep -i "80"|wc -l 2)统计httpd协议连接数ps -ef|grep httpd|wc ...

  10. sql update小结

    以前update用的不少,但都是简单的单表操作,没有在意,最近查阅多表关联更新及更新top n,发现update还真灵活,记录如下(在mssqlserver2008r2下测试通过): 1单表操作  u ...