Sudoku Solver [LeetCode]
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.
Summary: finds the cell which has the least candidates first, then checks all candidates in the cell.
vector<int> findLeastCandidate(vector<vector<char> > &board, vector<int> &out_digits){
vector<int> idx;
int candidate_num = ;
int n = board.size();
for(int i = ; i < n; i++){
for(int j = ; j < n; j ++) {
if(board[i][j] == '.'){
static const int arr[] = {,,,,,,,,};
vector<int> digits(arr, arr + sizeof(arr) / sizeof(arr[]));
//check row
for(int k = ; k < n; k ++){
if(board[i][k] != '.')
digits[board[i][k] - - ] = -;
}
//check column
for(int k = ; k < n; k ++){
if(board[k][j] != '.')
digits[board[k][j] - - ] = -;
}
//check the 9 sub-box
int row_box_idx = i/;
int column_box_idx = j/;
for(int l = row_box_idx * ; l < (row_box_idx + ) * ; l ++){
if(l == i)
continue;
for(int m = column_box_idx * ; m < (column_box_idx + ) * ; m ++){
if(board[l][m] != '.' && m != j)
digits[board[l][m] - - ] = -;
}
}
//caculate candidate number
int tmp_num = ;
for(int candidate: digits)
if(candidate != -)
tmp_num ++; if(tmp_num == ){
if(idx.size() == ){
idx.push_back(-);
idx.push_back(-);
}else {
idx[] = -;
idx[] = -;
}
return idx;
} if(tmp_num < candidate_num){
candidate_num = tmp_num;
out_digits = digits;
if(idx.size() == ){
idx.push_back(i);
idx.push_back(j);
}else {
idx[] = i;
idx[] = j;
}
}
}
}
}
return idx;
} bool isValidSudoku(vector<vector<char> > &board) {
//find the candidate which has most constrict
vector<int> digits;
vector<int> idx = findLeastCandidate(board, digits);
if(idx.size() == )
return true; if(idx[] == -)
return false; int i = idx[];
int j = idx[];
//recursive
bool is_all_minus = true;
for(int candidate: digits) {
if(candidate != -) {
is_all_minus = false;
board[i][j] = candidate + ;
if(isValidSudoku(board))
return true;
else
board[i][j] = '.';
}
}
if(is_all_minus)
return false;
return false;
} void solveSudoku(vector<vector<char> > &board) {
isValidSudoku(board);
}
Sudoku Solver [LeetCode]的更多相关文章
- Sudoku Solver leetcode java
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- Leetcode之回溯法专题-37. 解数独(Sudoku Solver)
Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...
- 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 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 ...
随机推荐
- change the walltime for currently running PBS job (qalter pbs)
qalter jobid -l walltime=X e.g.qalter 377470.manager -l walltime=2222:00:00qalter: Unauthorized Requ ...
- HTTP Status 404–/webDemo/hello
现在用一排很小的字写出来,我真是个大傻逼
- CSS笔记(七)列表
CSS 列表属性允许你放置.改变列表项标志,或者将图像作为列表项标志. 参考:http://www.w3school.com.cn/css/css_list.asp 实例: <html> ...
- Domion OA 日记
我现在使用的是IBM的 Lotus Dimion 8.5 以下内容是个人的浅显了解,在此记录下,已作为后续记录的翻看 第一次接触文档型数据库,确实颠覆了我对数据模型的认知,我之前一直用sql的 文档型 ...
- LTE Module User Documentation(翻译1)——背景、使用概述、基本的仿真程序和配置LTE模型参数
LTE用户文档 (如有不当的地方,欢迎指正!) 1.背景 假定读者已经熟悉 ns-3 simulator ,能运行一般的仿真程序.如果不是的话,强烈推荐读者参考 [ns3tutorial]. 2. ...
- JavaScript删除-confirm
一> onclick="javascript:if (confirm('您确定要删除吗?注意:此操作不可恢复,请谨慎操作!')){return true;} return false; ...
- RARP
ARP的工作原理如下:1. 首先,每台主机都会在自己的ARP缓冲区 (ARP Cache)中建立一个 ARP列表,以表示IP地址和MAC地址的对应关系.2. 当源主机需要将一个数据包要发送到目的主机时 ...
- iOS - 3DTouch 3D 触摸
1.3DTouch 简介 3DTouch 是 iOS9 + 系统下,在 iPhone6s(iPhone6s Plus)+ 手机上才能够使用的功能. 1.1 3DTouch 基本类型 1.主屏幕快速选项 ...
- 加载.properties方式
相对路径时注意:是相对项目(即包下)还是相对当前类(一般都是相对当前项目)(对于非class的资源文件eclipse编译时会直接放到bin目录下) 1.一般是从目录中加载:需要指明路径 2.另外就是通 ...
- postgresql如何实现group_concat功能
MySQL有个聚集函数group_concat, 它可以按group的id,将字段串联起来,如 表:id name---------------1 A2 B1 B SELECT id, group_c ...