Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. bool solveSudoku(vector< vector<char> >& board) {
  2. for (int i=0; i<9; ++i){
  3. for (int j=0; j<9; ++j){
  4. if (board[i][j]=='.'){
  5. for (int k=0; k<9; ++k){
  6. board[i][j]=('1'+k);
  7. if (check(i,j, board)){
  8. if (solveSudoku(board)){
  9. return true;
  10. }
  11. }
  12. }
  13. return false;
  14. }
  15. }
  16. }
  17. return true;
  18. }
  19. bool check(int i, int j, vector< vector<char> >& board){
  20. //check horizontal
  21. for (int l=0; l<9; ++l){
  22. if (board[i][l]==board[i][j] && l!=j){
  23. return false;
  24. }
  25. }
  26. //check vertical
  27. for (int l=0; l<9; ++l){
  28. if (board[l][j]==board[i][j] && l!=i){
  29. return false;
  30. }
  31. }
  32. //check block
  33. int block_x = i/3;
  34. int block_y = j/3;
  35. block_x*=3;
  36. block_y*=3;
  37. for(int l=0; l<3; ++l){
  38. for (int k=0; k<3; ++k){
  39. if (board[block_x+l][block_y+k]==board[i][j] && block_x+l!=i && block_y+k!=j){
  40. return false;
  41. }
  42. }
  43. }
  44. //all valid so return true
  45. return true;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement