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. Cheatsheet: 2014 02.01 ~ 02.28

    Database Managing disk space in MongoDB When to use GridFS on MongoDB .NET The Past, Present, and Fu ...

  2. Limiting To Select Only 5 Check Boxes Out Of Ten In Oracle Forms

    Suppose you want to give an option to user to select only 5 check boxes from given any number of che ...

  3. 软/硬链接指令:ln

    语法: ln  [选项]  原文件  目标文件 选项: -s 创建软连接(创建软链接时,若所在文件夹不一致,原文件要使用绝对路径) 硬链接特征: 1.拥有相同i节点和存储block块,可以看成是同一个 ...

  4. h5移动开发css

    最近刚开始做移动端的开发,接触到很多新的东西,很荣幸(*^__^*) , 下面我们开始正式介绍最近新接触到的属性啦,一起进步: 1.点击按钮等会产生阴影,可设置这个属性:-webkit-tap-hig ...

  5. 2----lua的入门

    Lua的标识符 什么是标识符? 标识符就是你给对象,函数等取的名字 区分大小写 首字母可以使字母下划线和美元符号组成 Lua 的保留字 保留字(区分大小写) true false and or not ...

  6. Scrum Meeting---One(2015-10-20)

    一.scrum meeting 在上周六我们团队进行了一次会议,讨论了我们团队的项目以及项目分工.首先是确立我们的项目,在团队的激烈讨论下我们决定做一个校园相关的APP.然后对于这个项目我们大致进行了 ...

  7. Android ActionBar以及menu的代码设置样式

    menu部分xml代码 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android= ...

  8. 专题实验 PGA

    PGA : 是完全为 server process 服务的, 在 server process 创建时被分配到, 在server process 终止时被释放. 而且是非共享的, 只独立服务于这个se ...

  9. poj1319Pipe Fitters

    链接 算不上几何的水题 第一种为(int)a*(int)b: 第二种分宽高交换两种讨论. 每一个的高度除第一个为1外其它的都可以看着b/sqrt(3.0)/2; #include <iostre ...

  10. ubuntu 14.0.04 通过自启动脚本实现主机和虚拟机的文件夹共享

    首先要先设置主机ip为固定ip: 1.vi /etc/rc.local,进入vi界面,在尾部增加一行代码: mount -t cifs -o username=share,password=12345 ...