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 ...
随机推荐
- jquery ajax请求时,设置请求头信息
设置一个名为 headers 的参数 参考代码: // attempt to make an XMLHttpRequest to indeed.com // jQuery 1.6.1 and Chro ...
- 机器学习——SVM详解(标准形式,对偶形式,Kernel及Soft Margin)
(写在前面:机器学习入行快2年了,多多少少用过一些算法,但由于敲公式太过浪费时间,所以一直搁置了开一个机器学习系列的博客.但是现在毕竟是电子化的时代,也不可能每时每刻都带着自己的记事本.如果可以掏出手 ...
- [51NOD1024] 矩阵中不重复的元素(数学,精度)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1024 因为n和m都到100了,所以直接快速幂硬算一定会爆炸,考 ...
- jquery动态样式操作
获取与设置样式 获取class和设置class都可以使用attr()方法来完成.例如使用attr()方法来获取p元素的class,JQuery代码如下: 1 var p_class = $(" ...
- servlet&jsp高级:第五部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- [Java解惑]数值表达式
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- CGAffineTransform与CATransform3D
CGAffineTransform 1.CG的前缀告诉我们,CGAffineTransform类型属于Core Graphics框架,Core Graphics实际上是一个严格意义上的2D绘图API, ...
- uploads 上传图片
public static function upFile($r,$p='../images/link/',$type='gif,jpg,png',$named=0){ $newnames = nul ...
- Android获取窗体信息的Util方法
package com.wangyi.tools; import android.app.Activity; import android.util.DisplayMetrics; public cl ...
- 慎重别选择到"僵尸"软件
何谓僵尸? 没有灵魂,动作单一,我们电视电影上经常看见, 只能往前跳,不会走路, 手向前伸直,左右摆攻击. 何谓"僵尸"软件? 根据僵尸的特性,大概有如下几类: 1.没有任何创新性 ...