#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. 手工删除oracle的方法

    大致方法如下:  1.删除物理文件     1.1.oracle安装文件.     1.2.系统目录下,program files文件下的oracle文件 2.注册表中大概有这么几个地方:  hkey ...

  2. c#的数据类型、运算符

    数据类型:整型:int short long byte小数:double float decimal布尔:bool字符:char 定义变量:数据类型 变量名 [= 值];变量名的命名规则:1.组成的字 ...

  3. 想使用WM_CONCAT 函数进行多列转一行,但发现没有

    查看数据库版本: SELECT * FROM v$version; 1 Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bi ...

  4. 在.NET中使用iTextSharp创建/读取PDF报告: Part I [翻译]

    原文地址:Create/Read Advance PDF Report using iTextSharp in C# .NET: Part I    By Debopam Pal, 27 Nov 20 ...

  5. c++试题

    一.写一个函数找一个字符串中出现频率最高的字符(若最高的相同,取先出现的) char finchar(const char *str) { ; } ]; , n = ; ; str[i]!=; i++ ...

  6. Javascript 拖拽的一些简单的应用——逐行分析代码,让你轻松了解拖拽的原理

    今天我们来看看如何让拖拽的物体不能拖出某个div之外和拖拽的吸附功能 上次讲到我们的拖拽是不可拖出可视区范围的,在这基础上我们加个父级的div,不让他拖出父级.原理和之前的一样,简单吧. <di ...

  7. python练习_购物车(2)

    用python写了一个购物车程序,主要是练习,代码如下 主入口文件:main.py #!/usr/bin/env python # -*- coding:utf-8 -*- #先调用用户登录函数,在进 ...

  8. python 初学笔记 (一)

    初学python第一天,希望自己真正了解计算机语言,并且做出成效. 写下学习笔记,记录学习进度,娱乐学习,不断成长. python详细介绍: python是什么?运用到哪里?有哪些在使用它? pyth ...

  9. wifi 攻破

    链接1 wifi 加密方式 1,wep加密 2.WPA/WPA2-PSK加密 WPA2 的破解方式: 1 爆力破解 2,pin 破解 1) 先破解 pin 码 2)再用 minidwep-gtk 破解

  10. html5 OPOA

    1.0 one page one application     一个应用只有一个页面,对用户的表现来说的. 2.0 起源于web的MIS系统     MIS(管理信息系统) 3.0 银行客户端 4. ...