LeetCode开心刷题二十一天——37. Sudoku Solver

男娘i 2023-08-17 15:14 170阅读 0赞
  1. Sudoku Solver

Hard

95062FavoriteShare

Write a program to solve a Sudoku puzzle by filling the empty cells.

A sudoku solution must satisfy all of the following rules:

  1. Each of the digits 1-9 must occur exactly once in each row.
  2. Each of the digits 1-9 must occur exactly once in each column.
  3. Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.

Empty cells are indicated by the character '.'.

250px-Sudoku-by-L2G-20050714.svg.png
A sudoku puzzle…

250px-Sudoku-by-L2G-20050714_solution.svg.png
…and its solution numbers marked in red.

Note:

  • The given board contain only digits 1-9 and the character '.'.
  • You may assume that the given Sudoku puzzle will have a single unique solution.
  • The given board size is always 9x9.

Idea Statement:like 8 queen problem,we have to use tranverse.but the way we easier this action is to add limitations to the tranverse.

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<vector>
  4. #include<algorithm>
  5. #include<stack>
  6. #include<string>
  7. #include<memory>
  8. #include<memory.h>
  9. #include<hash_map>
  10. #include<map>
  11. #include<set>
  12. #include<unordered_map>
  13. using namespace std;
  14. class Solution {
  15. public:
  16. void solveSudoku(vector<vector<char>>& board) {
  17. rows_ = vector<vector<int>>(9, vector<int>(10));
  18. cols_ = vector<vector<int>>(9, vector<int>(10));
  19. boxes_ = vector<vector<int>>(9, vector<int>(10));
  20. for(int i=0;i<9;i++)
  21. {
  22. for(int j=0;j<9;j++)
  23. {
  24. const char c=board[i][j];
  25. if(c!='.')
  26. {
  27. int n=c-'0';
  28. int bx=j/3;
  29. int by=i/3;
  30. rows_[i][n] = 1;
  31. cols_[j][n] = 1;
  32. boxes_[by * 3 + bx][n] = 1;
  33. }
  34. }
  35. }
  36. fill(board, 0, 0);
  37. }
  38. private:
  39. vector<vector<int>> rows_,cols_,boxes_;
  40. bool fill(vector<vector<char>>& board,int x,int y)
  41. {
  42. if(y==9) return true;
  43. int nx=(x+1)%9;
  44. int ny=(nx==0)?y+1:y;
  45. if(board[y][x]!='.') return fill(board,nx,ny);
  46. for(int i=1;i<=9;i++)
  47. {
  48. int bx=x/3;
  49. int by=y/3;
  50. int box_key=by*3+bx;
  51. if(!rows_[y][i]&&!cols_[x][i]&&!boxes_[box_key][i])
  52. {
  53. rows_[y][i]=1;
  54. cols_[x][i]=1;
  55. boxes_[box_key][i]=1;
  56. board[y][x]=i+'0';
  57. if(fill(board,nx,ny)) return true;
  58. board[y][x] = '.';
  59. boxes_[box_key][i] = 0;
  60. cols_[x][i] = 0;
  61. rows_[y][i] = 0;
  62. }
  63. }
  64. return false;
  65. }
  66. };
  67. int main()
  68. {
  69. vector<vector<char>> board={
  70. {
  71. '.','.','4','.','.','.','6','3','.'},
  72. {
  73. '.','.','.','.','.','.','.','.','.'},
  74. {
  75. '5','.','.','.','.','.','.','9','.'},
  76. {
  77. '.','.','.','5','6','.','.','.','.'},
  78. {
  79. '4','.','3','.','.','.','.','.','1'},
  80. {
  81. '.','.','.','7','.','.','.','.','.'},
  82. {
  83. '.','.','.','5','.','.','.','.','.'},
  84. {
  85. '.','.','.','.','.','.','.','.','.'},
  86. {
  87. '.','.','.','.','.','.','.','.','.'}
  88. };
  89. Solution s;
  90. s.solveSudoku(board);
  91. for(vector<vector<char>>::iterator ite=board.begin();ite!=board.end();++ite)
  92. {
  93. vector<char> temp=*ite;
  94. for(vector<char>::iterator itee=temp.begin();itee!=temp.end();++itee)
  95. {
  96. cout<<*itee;
  97. }
  98. cout<<endl;
  99. }
  100. // for(auto t:board)
  101. // {
  102. // cout<<t;
  103. // }
  104. return 0;
  105. }

转载于:https://www.cnblogs.com/Marigolci/p/11205083.html

发表评论

表情:
评论列表 (有 0 条评论,170人围观)

还没有评论,来说两句吧...

相关阅读