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.

  1. class Solution {
  2. public:
  3. void solveSudoku(vector<vector<char>> &board) {
  4. int line=,column=-;
  5. findNextEmpty(board,line,column);
  6. backtracking(board,line,column);
  7. }
  8.  
  9. bool backtracking(vector<vector<char>> &board,int line, int column)
  10. {
  11. int i=line, j = column;
  12. for(int k = ; k<=; k++)
  13. {
  14. if(!isValid(board,line,column,k+'')) continue;
  15. board[line][column] = k+'';
  16.  
  17. if(!findNextEmpty(board,line,column)) return true; //if no empty cell is found
  18. if(backtracking(board,line,column)) return true;
  19.  
  20. //backTracking
  21. line = i;
  22. column = j;
  23. }
  24.  
  25. //backTracking
  26. board[line][column] = '.';
  27. return false; //if no valid number is found
  28. }
  29.  
  30. //为回溯法写一个独立的check函数
  31. bool isValid(vector<vector<char>> &board, int line, int column, char value)
  32. {
  33. //check九宫格的一个格
  34. int upperBoard = line/ * ;
  35. int leftBoard = column/ * ;
  36. for(int i = ; i<; i++)
  37. {
  38. for(int j = ; j<; j++)
  39. {
  40. if(board[upperBoard+i][leftBoard+j] == value) return false;
  41. }
  42. }
  43.  
  44. //check 列
  45. for(int i = ; i<; i++)
  46. {
  47. if(board[line][i] == value) return false;
  48. }
  49.  
  50. //check行
  51. for(int i = ; i<; i++)
  52. {
  53. if(board[i][column] == value) return false;
  54. }
  55. return true;
  56. }
  57.  
  58. bool findNextEmpty(vector<vector<char>> &board, int &line, int &column){
  59. int i,j;
  60. for(j = column+; j < ; j++){
  61. if(board[line][j]!='.') continue;
  62.  
  63. column=j;
  64. return true;
  65. }
  66.  
  67. for(i = line+; i < ; i++){
  68. for(j = ; j < ; j++){
  69. if(board[i][j]!='.') continue;
  70.  
  71. line = i;
  72. column=j;
  73. return true;
  74. }
  75. }
  76.  
  77. return false;
  78. }
  79.  
  80. };

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. commons.httpclient-3.X.jar 和 httpclient-4.x.jar是个什么关系?

    最近看项目的代码,看到工程中有两个jar包张的很像,一个是commons.httpclient-3.1.jar,一个是httpclient4.2.1.jar,很纳闷,而且这两个包里都有HttpClie ...

  2. Linux修改本地时间

    1.Linux时间调整 1)安装ntp(目的同步时间) yum install ntp 2)修改文件 vi /etc/ntp.conf 添加 server ntp.sjtu.edu.cn perfer ...

  3. sklearn.externals import joblib模块保存和下载使用模型的用法实例

    #加载模块 from sklearn import datasets from sklearn.externals import joblib from sklearn.linear_model im ...

  4. 深入理解yield(三):yield与基于Tornado的异步回调

    转自:http://beginman.cn/python/2015/04/06/yield-via-Tornado/ 作者:BeginMan 版权声明:本文版权归作者所有,欢迎转载,但未经作者同意必须 ...

  5. Android Studio Ffmpeg

    1:编写java package com.example.zhaohu.test; public class MainActivity extends AppCompatActivity { prot ...

  6. java的缓存框架

    1.java里面有一些开源的缓存框架,比如ecache,memcache,redis等缓存框架. 2.使用缓存框架的原理就是减少数据库端的压力,将缓存数据放在内存里面,存储成键值对的格式,这样可以不去 ...

  7. 库、教程、论文实现,这是一份超全的PyTorch资源列表(Github 2.2K星)

    项目地址:https://github.com/bharathgs/Awesome-pytorch-list 列表结构: NLP 与语音处理 计算机视觉 概率/生成库 其他库 教程与示例 论文实现 P ...

  8. sqoop1 使用测试

    hive导入数据到mysql最简单的方式就是从hdfs直接读取hive表文件导入mysql,当然这需要知道数据表保存的目录 如果能直接从表到表的导入,无需路径,当然是最好了 1.需要下载合适的hive ...

  9. 39. 在linux下装好Tomcat要给 tomcat/bin/下面所有.sh的文件执行权限

    chmod a+x *.sh(赋予可执行的权限)

  10. 21OGNL与ValueStack(VS)-静态方法访问

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 在LoginAction中增加如下方法: public static Str ...