leetcde37. 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.
![]()
A sudoku puzzle...
![]()
...and its solution numbers marked in red.
class Solution {
private:
bool isValidSudoku(vector<vector<char>> & board, int row, int col,
char val) {
int N = board.size();
for (int i = ; i < N; i++) {
if (board[row][i] == val)
return false;
}
for (int i = ; i < N; i++) {
if (board[i][col] == val)
return false;
}
int r = row / * ;
int c = col / * ;
for (int i = r; i < r + ; i++) {
for (int j = c; j < c + ; j++) {
if (board[i][j] == val)
return false;
}
}
return true;
}
bool solveSudoku(vector<vector<char>>& board, int row, int col) {
int N = board.size();
if (row == N) return true;
if (col == N) return solveSudoku(board, row + , );
if (board[row][col] != '.')
return solveSudoku(board, row, col + );
for (char k = ''; k <= ''; k++) {
if (!isValidSudoku(board, row, col, k))
continue;
board[row][col] = k;
if(solveSudoku(board, row, col + ))
return true;
board[row][col] = '.';
}
return false;
}
public:
void solveSudoku(vector<vector<char>>& board) {
solveSudoku(board, , );
}
};
leetcde37. 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 ...
随机推荐
- Linux进程基础
Linux进程基础 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 计算机实际上可以做的事情实质上非常简单,比如计算两个数的和 ...
- HQL 参数绑定、唯一结果、分页、投影总结(上)
我们先总结一下HQL语句常用语法: from子句:; select子句:用于选取对象和属性; where子句:用于表达查询语句的限制条件; 使用表达式:一般用在where子句中; order by子句 ...
- python成长之路【第四篇】:装饰器
实现装饰器的知识储备: 示例: def f1(): print("f1") 1.函数即“变量” #上面的示例中,函数f1为变量,它指向内存地址.而f1()表示函数执行. 2.高阶函 ...
- Makefile-入门与进阶【转】
from:here 一.入门 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的 ...
- iOS开发 利用Reachability判断网络环境
导入头文件:#import "Reachability.h" 然后将 SystemConfiguration.framework 添加进工程: 1.检查当前的网络状态(wifi.W ...
- Python之路 day2 字符串函数
#Author:ersa name = "ersa" #首字母大写capitalize() print(name.capitalize()) name = "my nam ...
- (转)PowerDesigner提示Existence of index、key、reference错误
建立一个表后,为何检测出现Existence of index的警告 A table should contain at least one column, one index, one key ...
- 【bzoj1084】最大子矩阵
题意 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠. \(1≤n≤100,1≤m≤2,1≤k≤10\) 分析 由于\(m\)只有两 ...
- ubuntu下添加/删除启动服务项
在网上查了一下,命令如下 1.添加一个服务: $sudo update-rc.d ServiceName default 2.删除一个服务 $sudo update-rc.d ServiceName ...
- SPSS数据分析—分段回归
在SPSS非线性回归过程中,我们讲到了损失函数按钮可以自定义损失函数,但是还有一个约束按钮没有讲到,该按钮的功能是对自 定义的损失函数的参数设定条件,这些条件通常是由逻辑表达式组成,这就使得损失函数具 ...