[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 ...
随机推荐
- 游戏 slider
using UnityEngine; using System.Collections; public class La : MonoBehaviour { float verticalValue=0 ...
- Task<TResult> 类
https://msdn.microsoft.com/zh-cn/library/dd321424.aspx
- win95+ie3-win10+ie11 浏览器执行漏洞
alliedve.htm <!doctype html><html><meta http-equiv="X-UA-Compatible" conten ...
- MS10048依旧是Windows 2003 x86 的杀器
今天搞了个wow的游戏论坛,服务器环境是win03 x86+iis6.0+php+mysql. 提权的时候各种无奈,mysql无权限,而且没root,试了几个别的方法都不行,实在没办法的时候,用MS1 ...
- MemPool
腾讯笔试题,设计内存池,alloc和free都是O(1). 和LRUCache类似,这里用了一个list表示可用的空间,用一个map来记录这块内存是否已分配,这样free的时候才可能O(1). cla ...
- 【应用笔记】【AN002】通过iTool2基于MinGW平台读写EEPROM
为了增加大家 DIY 的乐趣,XiaomaGee今天为大家只做了一篇使用iTool2内置的USB转I2C来读写EEPROM的方法和代码. iTool2简介 iTool2为银杏公司面向电子类研发工程师推 ...
- Java集合之Collection接口
java的集合分为三大接口,分别是Collection,Map,Iterator,集合接口和类在java.util包中,此次主要介绍三大接口之一的Collection接口. 一些Collection允 ...
- NOJ 1643 阶乘除法(YY+小技巧)
[1643] 阶乘除法 时间限制: 5000 ms 内存限制: 65535 K 问题描述 输入两个正整数 n, m,输出 n!/m!,其中阶乘定义为 n!= 1*2*3*...*n (n>=1) ...
- 前端网址收集!Amazing! 神奇!
(1)Bootstrap中文网 http://www.bootcss.com/ (2)前端编码规范 http://codeguide.bootcss.com/ (3)bootstrap可视化布局+下载 ...
- nginx不支持pathinfo函数
server { listen ; server_name www.domain.com domain.com; error_page /.html; error_page /50x.html; lo ...