* Runtime - 56ms * Memory - 6.8 * LOGIC - described in functions * */
| 5 | * |
| 6 | */ |
| 7 | class Solution { |
| 8 | public: |
| 9 | |
| 10 | // to check whether num is valid in that row |
| 11 | bool isRowSafe(vector<vector<char>>& board, int row, char num) |
| 12 | { |
| 13 | for(int col = 0;col<9;col++) |
| 14 | { |
| 15 | if(board[row][col] == num) |
| 16 | return true; |
| 17 | } |
| 18 | return false; |
| 19 | } |
| 20 | |
| 21 | // to check whether num is valid in that column |
| 22 | bool isColumnSafe(vector<vector<char>>& board, int col , char num) |
| 23 | { |
| 24 | for(int row = 0; row<9; row++) |
| 25 | { |
| 26 | if(board[row][col] == num) |
| 27 | return true; |
| 28 | } |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | // to check whether num is valid in that 3*3 matrix grid |
| 33 | bool isBoxSafe(vector<vector<char>>& board, int row1, int col1 , char num) |
| 34 | { |
| 35 | for(int row = 0;row<3;row++) |
| 36 | { |
| 37 | for(int col = 0;col<3;col++) |
| 38 | { |
| 39 | if(board[row1 + row][col1 + col] == num) |
| 40 | return true; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | bool isSafe(vector<vector<char>>& board, int row, int col , char num) |
| 48 | { |
| 49 | return !isRowSafe(board,row,num) && !isColumnSafe(board, col, num) && !isBoxSafe(board, row-row%3,col-col%3,num) && board[row][col] == '.'; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | bool findUnassignedLocation(vector<vector<char>>& board, int &row, int &col) |
| 54 | { |
| 55 | for(row = 0;row<9;row++) |
| 56 | { |
| 57 | for(col = 0;col<9;col++) |
| 58 | { |
| 59 | if(board[row][col] == '.') |
| 60 | return true; |
| 61 | |
| 62 | } |
| 63 | } |
| 64 | return false; |
nothing calls this directly
no outgoing calls
no test coverage detected