37. Sudoku Solver (Array;Back-Track)
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)的更多相关文章
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- 36. Valid Sudoku + 37. Sudoku Solver
▶ 有关数独的两个问题. ▶ 36. 检测当前盘面是否有矛盾(同一行.同一列或 3 × 3 的小框内数字重复),而不关心该盘面是否有解. ● 初版代码,24 ms,没有将格子检测函数独立出去,行检测. ...
- [LeetCode] 37. Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- 37. Sudoku Solver
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...
- Java [leetcode 37]Sudoku Solver
题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- leetcode problem 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. Empty cells are indicated by th ...
随机推荐
- commons.httpclient-3.X.jar 和 httpclient-4.x.jar是个什么关系?
最近看项目的代码,看到工程中有两个jar包张的很像,一个是commons.httpclient-3.1.jar,一个是httpclient4.2.1.jar,很纳闷,而且这两个包里都有HttpClie ...
- Linux修改本地时间
1.Linux时间调整 1)安装ntp(目的同步时间) yum install ntp 2)修改文件 vi /etc/ntp.conf 添加 server ntp.sjtu.edu.cn perfer ...
- sklearn.externals import joblib模块保存和下载使用模型的用法实例
#加载模块 from sklearn import datasets from sklearn.externals import joblib from sklearn.linear_model im ...
- 深入理解yield(三):yield与基于Tornado的异步回调
转自:http://beginman.cn/python/2015/04/06/yield-via-Tornado/ 作者:BeginMan 版权声明:本文版权归作者所有,欢迎转载,但未经作者同意必须 ...
- Android Studio Ffmpeg
1:编写java package com.example.zhaohu.test; public class MainActivity extends AppCompatActivity { prot ...
- java的缓存框架
1.java里面有一些开源的缓存框架,比如ecache,memcache,redis等缓存框架. 2.使用缓存框架的原理就是减少数据库端的压力,将缓存数据放在内存里面,存储成键值对的格式,这样可以不去 ...
- 库、教程、论文实现,这是一份超全的PyTorch资源列表(Github 2.2K星)
项目地址:https://github.com/bharathgs/Awesome-pytorch-list 列表结构: NLP 与语音处理 计算机视觉 概率/生成库 其他库 教程与示例 论文实现 P ...
- sqoop1 使用测试
hive导入数据到mysql最简单的方式就是从hdfs直接读取hive表文件导入mysql,当然这需要知道数据表保存的目录 如果能直接从表到表的导入,无需路径,当然是最好了 1.需要下载合适的hive ...
- 39. 在linux下装好Tomcat要给 tomcat/bin/下面所有.sh的文件执行权限
chmod a+x *.sh(赋予可执行的权限)
- 21OGNL与ValueStack(VS)-静态方法访问
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 在LoginAction中增加如下方法: public static Str ...