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]的更多相关文章

  1. Sudoku Solver leetcode java

    题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...

  2. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  3. [Leetcode][Python]37: Sudoku Solver

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...

  4. Leetcode之回溯法专题-37. 解数独(Sudoku Solver)

    Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...

  5. LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  6. [leetcode]算法题目 - Sudoku Solver

    最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...

  7. 【leetcode】Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  8. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

  9. 【LeetCode】37. Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

随机推荐

  1. SQLServer使用表值参数,高性能批量插入数据

    记得前段时间帮同事写了个解析账号并入库的小工具,来批量导入账号信息,账号量相当大,程序每读取一条记录便执行一次insert来插入数据,整整跑了一下午才把账号全部入库. 今天又接到同事类似的需求,不过这 ...

  2. Sql合并两个select查询

    现有2个查询,需要将每个查询的结果合并起来(注意不是合并结果集,因此不能使用union),可以将每个查询的结果作为临时表,然后再从临时表中select所需的列,示例如下:   SELECT get.d ...

  3. ubuntu中rar与unrar用法详解

    本文转载:http://helloklzs.iteye.com/blog/1139993 安装: sudo apt-get install rar 这样就可以安装了 删除是以下语句 sudo apt- ...

  4. Linux 新手非常有用的命令

    http://www.cnblogs.com/felix-/p/4341773.html Linux 新手非常有用的命令 你打算从Windows换到Linux上来,还是你刚好换到Linux上来?哎哟! ...

  5. Task<TResult>的使用

    https://msdn.microsoft.com/en-us/library/dd321424(v=vs.110).aspx Represents an asynchronous operatio ...

  6. printf()函数

    printf()函数是格式化输出函数, 一般用于向标准输出设备按规定格式输出信息. printf()函数的调用格式为: printf("<格式化字符串>", <参 ...

  7. Beaglebone Black– 智能家居控制系统 LAS - 网页服务器 Node.js 、Web Service、页面 和 TCP 请求转 UDP 发送

    上一篇,纯粹玩 ESP8266,写入了 init.lua 能收发 UDP.这次拿 BBB 开刀,用 BBB host 一个 web server ,用于与用户交互,数据来自 ESP8266 的 UDP ...

  8. PostgresSql开放局域网访问

    1) 确认已经退掉所有的MASF终端和MSF GUI,然后打开PostgresSQL的启动文件在文件POSTGRESQL_START参数后面添加-h 0.0.0.0,让PostgreSQL启动时绑定到 ...

  9. hdu 2199 (二分)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2199 Can you solve this equation? Time Limit: 2000/1000 ...

  10. So easy Webservice 1.Socket建设web服务

    socket 是用来进行网络通讯的,简单来说,远程机器和本地机器各建一个socket,然后通过该socket进行连接通讯 socket简单模型图: socket的原理图: 代码实现: 1.创建sock ...