[LeetCode]题解(python):037-Sudoku Solver
题目来源
https://leetcode.com/problems/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.
题意分析
Input:a unsolved Sudoku
Output: a solved Sudoku
Conditions:满足数独条件,只需要找到一个答案
题目思路
用dfs的思路,对每一个空进行1-9的选择,如果不满足则回溯;满足条件为isValid(),就是满足该行,该列以及该小九方格没有使用过两个一样的元素(即数字)
AC代码(Python)
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):
temp = board[x][y]
board[x][y] = 'r'
for i in range(9):
if board[i][y] == temp:
return False
for i in range(9):
if board[x][i] == temp:
return False
for i in range(3):
for j in range(3):
if board[x / 3 * 3 + i][y / 3 * 3 + j] == temp:
return False
board[x][y] = temp
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]题解(python):037-Sudoku Solver的更多相关文章
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- LeetCode 037 Sudoku Solver
题目要求:Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells ...
- Java for LeetCode 037 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
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...
- 037 Sudoku Solver 解数独
写一个程序通过填充空格来解决数独.空格用 '.' 表示. 详见:https://leetcode.com/problems/sudoku-solver/description/ class Solut ...
- LeetCode(37) Sudoku Solver
题目 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by ...
- [Leetcode 37]*数独游戏 Sudoku Solver 附解释
[题目] 每一行.每一列.每个3*3的格子里只能出现一次1~9. [思路] 参考了思路,附加了解释. dfs遍历所有非空格子,n是已经填好的个数. 初始化条件.n=81,都填了,返回结束.对于已经填好 ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- Leetcode之回溯法专题-37. 解数独(Sudoku Solver)
Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...
随机推荐
- Cat VS Dog
Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Total ...
- CSS3 选择器——伪类选择器
前面花了两节内容分别在<CSS3选择器——基本选择器>和<CSS3选择器——属性选择器>介绍了CSS3选择器中的基本选择器和属性选择器使用方法,今天要和大家一起学习CSS3选择 ...
- 【BZOJ】1303: [CQOI2009]中位数图(特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1303 依旧是题解流,,,不看题解没法活,,,第一眼就是瞎搞,然后就是暴力,显然TLE..题解啊题解. ...
- 【BZOJ】1192: [HNOI2006]鬼谷子的钱袋(水题)
http://www.lydsy.com/JudgeOnline/problem.php?id=1192 看到题我就好像想起以前小学升学考数学的最后一题,将一条金块分割最少的部分,使得每一天都能够支付 ...
- DWR入门教程
DWR(Direct Web Remoting)是一个WEB远程调用框架.利用这个框架可以让AJAX开发变得很简单.利用DWR可以在客户端利用JavaScript直接调用服务端的Java方法并返回值给 ...
- java获取获得Timestamp类型的当前系统时间
java获取取得Timestamp类型的当前系统时间java获取取得Timestamp类型的当前系统时间 格式:2010-11-04 16:19:42 方法1: Timestamp d = new T ...
- salt执行报错一例
执行报错: 查看服务端日志: 认证有问题 重新认证吧!!! minion端: [root@super66 ~]# cd /etc/salt/[root@super66 salt]# lsminion ...
- Excel操作类
'引入Excel的COM组件 Imports System Imports System.Data Imports System.Configuration Imports System.Web Im ...
- Careercup | Chapter 7
7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only ...
- IMAP命令
IMAP命令学习1.CREATE <folder>CREATE可以创建指定名字的新邮箱.邮箱名称通常是带路径的文件夹全名.(有些IMAP客户机使用邮件夹称呼新邮箱)C: A003 CREA ...