LeetCode OJ-- Sudoku Solver ***
https://oj.leetcode.com/problems/sudoku-solver/
九宫格数独问题。
一行上为1 2 3 到9
一列上为1 2 3 到9
每个小的3*3格子为 1 2 3 到9
使用深搜递归实现:
在技巧上,使用类的成员变量 vector<vector<char> > board; 这样不用每层递归都传递这个棋盘了
使用类的成员变量 bool flagOK; 标志是否已找到 solution.
从(0,0)位置开始递归,一个个位置加,直到位置到了最后一个。
在每一层上,遍历1 2 3 到 9 ,看看哪个数可以往这里放,然后递归下一层,即下一个位置。
当从下一个位置返回的时候,要把之前设置的值再 恢复 回来
还有个情况会导致返回到上一次,那就是从1 到 9 都遍历完了,仍然都不可以,也要记得恢复回来
class Solution {
public:
vector<vector<char> > board;
bool flagOK;
void solveSudoku(vector<vector<char> > &board) {
if(board.empty())
return;
flagOK = false;
this->board = board;
subSudoku(,);
board = this->board;
} // in rows and cols don't have the same one
bool valide(char num, int p, int q)
{
bool flag = true;
for(int i = ; i < board.size(); i++)
{
if(num == board[i][q])
{
flag = false;
break;
}
}
if(flag)
{
for(int j = ; j < board.size(); j++)
{
if(num == board[p][j])
{
flag = false;
break;
}
}
}
return flag;
} void subSudoku(int i, int j)
{
// till end find solution
if(i == board.size())
{
flagOK = true;
return;
} // num placed
if(board[i][j] != '.')
{
if(j < board.size() - )
{
subSudoku(i,j + );
if(flagOK == true)
return; //不再递归了,已经找到答案了
}
else
{
subSudoku(i+,);
if(flagOK == true)
return;
}
}
else
{
char num;
for(num = ''; num <= ''; num++)
{
// in its small cube
bool OKSmallCube = true;
int smallCubeI = i/;
int smallCubeJ = j/;
// check small cube
for(int s = ; s < ; s++)
{
for(int m = ; m < ; m++)
{
if(board[smallCubeI* + s][smallCubeJ* + m] == num)
{
OKSmallCube = false;
break;
}
}
if(OKSmallCube == false)
break;
} if(OKSmallCube && valide(num,i,j))
{
board[i][j] = num;
if(j < board.size() - )
{
subSudoku(i,j + );
if(flagOK == true)
return;
}
else
{
subSudoku(i+,);
if(flagOK == true)
return;
}
}
board[i][j] = '.'; //注意恢复原样
}
board[i][j] = '.';//注意恢复原样
}
}
};
LeetCode OJ-- 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 037 Sudoku Solver
题目要求:Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells ...
- [LeetCode] 37. Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- Java for LeetCode 037 Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- Java [leetcode 37]Sudoku Solver
题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- [leetcode]37. Sudoku Solver 解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- LeetCode 37 Sudoku Solver(求解数独)
题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description Problem : 解决数独问题,给出一个二维数组,将这个数独 ...
- LeetCode 36 Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- [leetcode 37]sudoku solver
1 题目: 根据给出的数独,全部填出来 2 思路: 为了做出来,我自己人工做了一遍题目给的数独.思路是看要填的数字横.竖.子是否已经有1-9的数字,有就剔除一个,最后剩下一个的话,就填上.一遍一遍的循 ...
随机推荐
- Python基础:条件与循环
条件语句 除了 boolean 类型的数据,条件判断最好是显性的 if i != 0: ... 而不是只写出变量名: if i: ... For循环与While循环 通常来说,如果你只是遍历一个已知的 ...
- 常见的js算法面试题收集,es6实现
1.js 统计一个字符串出现频率最高的字母/数字 let str = 'asdfghjklaqwertyuiopiaia'; const strChar = str => { let strin ...
- python列表中的赋值与深浅拷贝
首先创建一个列表 a=[[1,2,3],4,5,6] 一.赋值 a=[[1,2,3],4,5,6]b=aa[0][1]='tom'print(a)print(b)结果: [[1, 'tom', 3], ...
- 分数调查 HihoCoder - 1515
小Hi的学校总共有N名学生,编号1-N.学校刚刚进行了一场全校的古诗文水平测验. 学校没有公布测验的成绩,所以小Hi只能得到一些小道消息,例如X号同学的分数比Y号同学的分数高S分. 小Hi想知道利用这 ...
- ACM-ICPC 2018 徐州赛区网络预赛
A. Hard to prepare #include <bits/stdc++.h> using namespace std; ; ]; ]; int main() { int T; i ...
- A1082 Read Number in Chinese (25)(25 分)
A1082 Read Number in Chinese (25)(25 分) Given an integer with no more than 9 digits, you are suppose ...
- complex类的定义、实现
复数类complex的定义.实现(求模.复数加法) #include <iostream> #include <cmath> using namespace std; clas ...
- Jquery查询分析器
find() 方法获得当前元素集合中每个元素的后代,通过选择器.jQuery 对象或元素来筛选.$(this).find("ul[index=1] div input:radio:check ...
- 矩阵儿快速幂 - POJ 3233 矩阵力量系列
不要管上面的标题的bug 那是幂的意思,不是力量... POJ 3233 Matrix Power Series 描述 Given a n × n matrix A and a positive in ...
- leetcode 【 Search Insert Position 】python 实现
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...