leetcode113:sudoku-solver
题目描述


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 {
public
:
void
solveSudoku(vector<vector<
char
>>& board) {
vector<vector<
int
>> used1(9,vector<
int
>(9,0)),used2(9,vector<
int
>(9,0)),used3(9,vector<
int
>(9,0));
fillnum(used1,used2,used3,board);
solve(used1,used2,used3,board);
}
void
fillnum(vector<vector<
int
>> &used1,vector<vector<
int
>> &used2,vector<vector<
int
>> &used3,vector<vector<
char
> > &board) {
for
(
int
i=0;i<board.size();i++)
for
(
int
j=0;j<board[0].size();j++)
if
(board[i][j]!=
'.'
)
{
int
num=board[i][j]-
'0'
-1;
int
k=i/3*3+j/3;
used1[i][num]=used2[j][num]=used3[k][num]=1;
}
}
bool
solve(vector<vector<
int
>> &used1,vector<vector<
int
>> &used2,vector<vector<
int
>> &used3,vector<vector<
char
>>&board){
for
(
int
i=0;i<board.size();i++)
for
(
int
j=0;j<board[0].size();j++){
int
k=i/3*3+j/3;
if
(board[i][j]==
'.'
){
for
(
int
fill=1;fill<10;fill++){
if
(used1[i][fill-1]==0 && used2[j][fill-1]==0 && used3[k][fill-1]==0){
board[i][j]=fill+
'0'
;
used1[i][fill-1]=used2[j][fill-1]=used3[k][fill-1]=1;
if
(solve(used1,used2,used3,board))
return
true
;
board[i][j]=
'.'
;
used1[i][fill-1]=used2[j][fill-1]=used3[k][fill-1]=0;
}
}
return
false
;
}
}
return
true
;
}
};
leetcode113:sudoku-solver的更多相关文章
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- [leetcode]算法题目 - Sudoku Solver
最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...
- 【leetcode】Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- Valid Sudoku&&Sudoku Solver
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- LeetCode解题报告—— Reverse Nodes in k-Group && Sudoku Solver
1. Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ...
- Leetcode之回溯法专题-37. 解数独(Sudoku Solver)
Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...
随机推荐
- JVM系列【5】JVM常用指令
JVM系列笔记目录 虚拟机的基础概念 class文件结构 class文件加载过程 jvm内存模型 JVM常用指令 GC与调优 指令集分类 基于寄存器的指令集 基于栈的指令集 Hotspot中的Loca ...
- 以太坊PoW
ethash ethash(eth+hash)是以太坊设计的挖矿算法,为了实现ASIC-resistance,ethash依赖于对内存资源的访问,是一种memory-hard函数.同时为了支持轻节点对 ...
- pytest文档40-pytest.ini配置用例查找规则(面试题)
前言 面试题:pytest如何执行不是test开头的用例?如执行 xxx_*.py这种文件的用例. pytest.ini 配置文件可以修改用例的匹配规则. pytest命令行参数 cmd打开输入pyt ...
- RDS 事务型数据库sql
-- 替换json中数据 select SUBSTRING_INDEX(SUBSTRING_INDEX('[{"channelCode":"MOBIL",&qu ...
- centos8使用systemd/systemctl管理系统/服务
一,systemd的用途? Systemd 是 Linux 系统工具,用来启动守护进程,已成为大多数发行版的标准配置 Systemd 的优点是功能强大,使用方便, 缺点是体系庞大,非常复杂 在cent ...
- 浅谈ES6——ES6中let、const、var三者的区别
在了解let.const.var的区别之前,先了解一些什么是es6 Es6 全称ECMAscript 是JavaScript语言的一个标准,其实Es6本质就是JavaScript的一个版本,为什么叫E ...
- 使用websocket连接(对接)asp.net core signalr
使用通用websocket连接asp.net core signalr 一.背景介绍 signalr的功能很强大,可以为我们实现websocket服务端节省不少的时间.但是可能由于不同的环境,我们在对 ...
- springcloud中使用dubbo开发rpc服务及调用
spring cloud中基于springboot开发的微服务,是基于http的rest接口,也可以开发基于dubbo的rpc接口. 一,创建goodsService模块 1, 在创建的goodsSe ...
- VirtualBox 安装Ubuntu(16.04/18.04)时显示不全的解决方法
是是系统分辨率不同导致的问题 Alt+鼠标左键 (16.04版本亲测有效,18.04版本亲测无效)或者Win+鼠标左键 (18.04版本亲测有效)拖动安装界面,即可显示内容.
- Docker指令整理
date: 2018-11-18 11:09:28 updated: 2018-11-18 11:09:28 Docker指令整理 管理员权限!!! 查看docker版本 docker -v 启动 s ...