Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. class Solution {
  2. public:
  3. bool isValidSudoku(vector<vector<char>>& board)
  4. {
  5. uint8_t i = 0, j = 0, k = 0, l = 0, x = 0, y = 0;
  6. vector<char> blk;
  7. //todo: check sizes of vectors
  8.  
  9. //rows
  10. for(i = 0; i < 9; ++i)
  11. {
  12. if(!isValidBlk(board[i]))
  13. {
  14. return false;
  15. }
  16. }
  17.  
  18. //cols
  19. for(i = 0; i < 9; ++i)
  20. {
  21. blk.clear();
  22. for(j = 0; j < 9; ++j)
  23. {
  24. blk.push_back(board[i][j]);
  25. }
  26.  
  27. if(!isValidBlk(blk))
  28. {
  29. return false;
  30. }
  31. }
  32.  
  33. //boxes
  34. // i,j
  35. // 0,0 1,0 2,0
  36. // 0,1 1,1 2,1
  37. // 0,2 1,2 2,2
  38. for(i = 0; i < 3; ++i)
  39. {
  40. blk.clear();
  41. y = 3 * i;
  42. for(j = 0; j < 3; ++j)
  43. {
  44. x = 3 * j;
  45. for(k = 0; k < 3; ++k)
  46. {
  47. for(l = 0; l < 3; ++l)
  48. {
  49. blk.push_back(board[x+k][y+l]);
  50. }
  51. }
  52. }
  53.  
  54. if(!isValidBlk(blk))
  55. {
  56. return false;
  57. }
  58. }
  59.  
  60. return true;
  61. }
  62. private:
  63. bool isValidBlk(vector<char>& blk)
  64. {
  65. char values[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  66. uint8_t index = 0, i;
  67.  
  68. for(i = 0; i < 9; ++i)
  69. {
  70. index = isValidNum(blk[i]) ? (blk[i] - '0') : 0;
  71. if(index && //only check if not '.' (short circuit)
  72. values[index] == 0)
  73. {
  74. return false;
  75. }
  76. values[index] = 0;
  77. }
  78. return true;
  79. }
  80.  
  81. bool isValidNum(uint8_t input) {
  82. if (input >= '1' && input <= '9')
  83. {
  84. return true;
  85. }
  86. return false;
  87. }
  88. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement