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 {
public:
void solveSudoku(vector<vector<char>> &board) {
int line=,column=-;
findNextEmpty(board,line,column);
backtracking(board,line,column);
} bool backtracking(vector<vector<char>> &board,int line, int column)
{
int i=line, j = column;
for(int k = ; k<=; k++)
{
if(!isValid(board,line,column,k+'')) continue;
board[line][column] = k+''; if(!findNextEmpty(board,line,column)) return true; //if no empty cell is found
if(backtracking(board,line,column)) return true; //backTracking
line = i;
column = j;
} //backTracking
board[line][column] = '.';
return false; //if no valid number is found
} //为回溯法写一个独立的check函数
bool isValid(vector<vector<char>> &board, int line, int column, char value)
{
//check九宫格的一个格
int upperBoard = line/ * ;
int leftBoard = column/ * ;
for(int i = ; i<; i++)
{
for(int j = ; j<; j++)
{
if(board[upperBoard+i][leftBoard+j] == value) return false;
}
} //check 列
for(int i = ; i<; i++)
{
if(board[line][i] == value) return false;
} //check行
for(int i = ; i<; i++)
{
if(board[i][column] == value) return false;
}
return true;
} bool findNextEmpty(vector<vector<char>> &board, int &line, int &column){
int i,j;
for(j = column+; j < ; j++){
if(board[line][j]!='.') continue; column=j;
return true;
} for(i = line+; i < ; i++){
for(j = ; j < ; j++){
if(board[i][j]!='.') continue; line = i;
column=j;
return true;
}
} return false;
} };

37. Sudoku Solver (Array;Back-Track)的更多相关文章

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

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

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

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

  3. 【LeetCode】37. Sudoku Solver

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

  4. 36. Valid Sudoku + 37. Sudoku Solver

    ▶ 有关数独的两个问题. ▶ 36. 检测当前盘面是否有矛盾(同一行.同一列或 3 × 3 的小框内数字重复),而不关心该盘面是否有解. ● 初版代码,24 ms,没有将格子检测函数独立出去,行检测. ...

  5. [LeetCode] 37. Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

  6. 37. Sudoku Solver

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

  7. Java [leetcode 37]Sudoku Solver

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

  8. leetcode problem 37 -- Sudoku Solver

    解决数独 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...

  9. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

随机推荐

  1. C# 窗体控件输入框大写

    // 将 a-z 改为 A-Z // 'a' 'z' && e.KeyChar <= ) e.KeyChar = ();

  2. Valgrind memcheck 8种错误实例

    调不尽的内存泄漏,用不完的Valgrind Valgrind 安装 1. 到www.valgrind.org下载最新版valgrind-3.2.3.tar.bz2 2. 解压安装包:tar –jxvf ...

  3. 廖雪峰Java1-2程序基础-7布尔运算符

    布尔运算符 关系运算符:>, >=, <, <=, ==,!= 与运算 && 或运算 | 非运算 ! int n = 5; boolean t = n > ...

  4. 9-16Jenkins-2定时任务

    定时任务 选择定时任务,点击 "?" 会弹出使用教程,但讨厌英语的人,自然选择现成的中文. 猪脚踏浪https://www.cnblogs.com/zsg88/p/9178625. ...

  5. 1112 Stucked Keyboard (20 分)

    1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when you ...

  6. move操作

    move一个表到另外一个表空间时,索引不会跟着一起move,而且会失效.(LOB类型例外) 表move,我们分为: *普通表move *分区表move *LONG,LOB大字段类型move来进行测试和 ...

  7. 建造者模式(Builder)

    Separate the construction of a complex object form its representation so that the same construction ...

  8. msq_table's methods2

    -- 删除数据 自增长id被占用 -- 清楚所有数据并重置id 1 truncate table name; -- 主键(唯一) id int primary key; -- 主键内容不能重复,不能为 ...

  9. Where is virtualenvwrapper.sh after pip install?

      I'm trying to setup virtualenvwrapper on OSX, and all the instructions and tutorials I've found te ...

  10. 3.纯 CSS 创作一个容器厚条纹边框特效

    原文地址:3.纯 CSS 创作一个容器厚条纹边框特效 没有啥好点子呀,不爽 HTML代码: <div class="box"> <div class=" ...