Advertisement
cincout

Untitled

Feb 21st, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. bool check(std::vector<std::vector<int>>& a, int x, int y) {
  2. int n = a.size(), m = a[0].size();
  3. for(int i = 0; i < n; ++i) {
  4. if(i != x && a[i][y] == a[x][y]) return false;
  5. }
  6. for(int j = 0; j < m; ++j) {
  7. if(j != y && a[x][j] == a[x][y]) return false;
  8. }
  9. int x2 = x - x % 3;
  10. int y2 = y - y % 3;
  11. for(int i = 0; i < 3; ++i) {
  12. for(int j = 0; j < 3; ++j) {
  13. int x1 = x2 + i;
  14. int y1 = y2 + j;
  15. if((x1 != x || y1 != y) && a[x][y] == a[x1][y1]) return false;
  16. }
  17. }
  18. return true;
  19. }
  20.  
  21.  
  22. bool go(std::vector<std::vector<int>>& a, int x, int y) {
  23. int n = a.size(), m = a[0].size();
  24. if(x == n) {
  25. return true;
  26. }
  27. if(y == m) {
  28. return go(a, x + 1, 0);
  29. }
  30. if(a[x][y] != 0) {
  31. return go(a, x, y + 1);
  32. }
  33.  
  34. for(int c = 1; c <= 9; ++c) {
  35. a[x][y] = c;
  36. if(check(a, x, y) && go(a, x, y + 1)) {
  37. return true;
  38. }
  39. }
  40. a[x][y] = 0;
  41.  
  42. return false;
  43. }
  44.  
  45.  
  46. std::vector<std::vector<int>> Solution::solve(std::vector<std::vector<int>>& matrix) {
  47. go(matrix, 0, 0);
  48. return matrix;
  49. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement