πŸ•·οΈ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 103 (from laksa062)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

πŸ“„
INDEXABLE
βœ…
CRAWLED
17 hours ago
πŸ€–
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://www.geeksforgeeks.org/dsa/sudoku-backtracking-7/
Last Crawled2026-04-10 14:34:05 (17 hours ago)
First Indexed2025-06-12 18:09:53 (10 months ago)
HTTP Status Code200
Meta TitleSudoku Solver - GeeksforGeeks
Meta DescriptionYour All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more., Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Meta Canonicalnull
Boilerpipe Text
#include <iostream> #include <vector> using namespace std ; ​ bool isSafe ( vector < vector < int >> & mat , int i , int j , int num , vector < int > & row , vector < int > & col , vector < int > & box ) { if ( ( row [ i ] & ( 1 << num )) || ( col [ j ] & ( 1 << num )) || ( box [ i / 3 * 3 + j / 3 ] & ( 1 << num )) ) return false ; return true ; } ​ bool sudokuSolverRec ( vector < vector < int >> & mat , int i , int j , vector < int > & row , vector < int > & col , vector < int > & box ) { int n = mat . size (); ​ if ( i == n - 1 && j == n ) return true ; ​ if ( j == n ) { i ++ ; j = 0 ; } if ( mat [ i ][ j ] != 0 ) return sudokuSolverRec ( mat , i , j + 1 , row , col , box ); ​ for ( int num = 1 ; num <= n ; num ++ ) { if ( isSafe ( mat , i , j , num , row , col , box )) { mat [ i ][ j ] = num ; row [ i ] |= ( 1 << num ); col [ j ] |= ( 1 << num ); box [ i / 3 * 3 + j / 3 ] |= ( 1 << num ); if ( sudokuSolverRec ( mat , i , j + 1 , row , col , box )) return true ; mat [ i ][ j ] = 0 ; row [ i ] &= ~ ( 1 << num ); col [ j ] &= ~ ( 1 << num ); box [ i / 3 * 3 + j / 3 ] &= ~ ( 1 << num ); } } return false ; } ​ void solveSudoku ( vector < vector < int >> & mat ) { int n = mat . size (); vector < int > row ( n , 0 ), col ( n , 0 ), box ( n , 0 ); ​ for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( mat [ i ][ j ] != 0 ) { row [ i ] |= ( 1 << mat [ i ][ j ]); col [ j ] |= ( 1 << mat [ i ][ j ]); box [ ( i / 3 ) * 3 + j / 3 ] |= ( 1 << mat [ i ][ j ]); } } } ​ sudokuSolverRec ( mat , 0 , 0 , row , col , box ); } ​ int main () { vector < vector < int >> mat = { { 3 , 0 , 6 , 5 , 0 , 8 , 4 , 0 , 0 }, { 5 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }, { 0 , 8 , 7 , 0 , 0 , 0 , 0 , 3 , 1 }, { 0 , 0 , 3 , 0 , 1 , 0 , 0 , 8 , 0 }, { 9 , 0 , 0 , 8 , 6 , 3 , 0 , 0 , 5 }, { 0 , 5 , 0 , 0 , 9 , 0 , 6 , 0 , 0 }, { 1 , 3 , 0 , 0 , 0 , 0 , 2 , 5 , 0 }, { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 4 }, { 0 , 0 , 5 , 2 , 0 , 6 , 3 , 0 , 0 }}; ​ solveSudoku ( mat ); for ( int i = 0 ; i < mat . size (); i ++ ) { for ( int j = 0 ; j < mat . size (); j ++ ) cout << mat [ i ][ j ] << " " ; cout << endl ; } ​ return 0 ; }
Markdown
[![geeksforgeeks](https://media.geeksforgeeks.org/gfg-gg-logo.svg)](https://www.geeksforgeeks.org/) ![search icon](https://media.geeksforgeeks.org/auth-dashboard-uploads/Property=Light---Default.svg) - Sign In - [Courses]() - [Tutorials]() - [Interview Prep]() - [DSA Tutorial](https://www.geeksforgeeks.org/dsa/dsa-tutorial-learn-data-structures-and-algorithms/) - [Interview Questions](https://www.geeksforgeeks.org/dsa/top-100-data-structure-and-algorithms-dsa-interview-questions-topic-wise/) - [Quizzes](https://www.geeksforgeeks.org/dsa/data-structures-and-algorithms-online-quiz/) - [Must Do](https://www.geeksforgeeks.org/dsa/must-do-coding-questions-for-companies-like-amazon-microsoft-adobe/) - [Advanced DSA](https://www.geeksforgeeks.org/dsa/advanced-data-structures/) - [System Design](https://www.geeksforgeeks.org/system-design/system-design-tutorial/) - [Aptitude](https://www.geeksforgeeks.org/aptitude/aptitude-questions-and-answers/) - [Puzzles](https://www.geeksforgeeks.org/aptitude/puzzles/) - [Interview Corner](https://www.geeksforgeeks.org/interview-prep/interview-corner/) - [DSA Python](https://www.geeksforgeeks.org/dsa/python-data-structures-and-algorithms/) # Sudoku Solver Last Updated : 27 Sep, 2025 Given an incomplete Sudoku in the form of matrix ****mat\[\]\[\]**** of order 9\*9, Complete the Sudoku. A sudoku solution must satisfy ****all of the following rules****: - Each of the digits 1-9 must occur exactly once in each row. - Each of the digits 1-9 must occur exactly once in each column. - Each of the digits 1-9 must occur exactly once in each of the 9, 3x3 sub-boxes of the grid. ****Note:**** Zeros in the mat\[\]\[\] indicate blanks, which are to be filled with some number between 1 to 9. You can not replace the element in the cell which is not blank. ****Examples:**** > ****Input:**** > > ![Suduko-example-question](https://media.geeksforgeeks.org/wp-content/uploads/20250131132344574720/Suduko-example-question.webp) > > ****Output:**** > > ![Suduko-example-answer](https://media.geeksforgeeks.org/wp-content/uploads/20250131132422988685/Suduko-example-answer.webp) > > ****Explanation:**** Each row, column and 3\*3 box of the output matrix contains unique numbers. [Try it on GfG Practice ![redirect icon](https://media.geeksforgeeks.org/auth-dashboard-uploads/Group-arrow.svg)](https://www.geeksforgeeks.org/problems/solve-the-sudoku-1587115621/1) Table of Content - [\[Approach 1\] Using Backtracking](https://www.geeksforgeeks.org/dsa/sudoku-backtracking-7/#naive-approach-using-backtracking) - [\[Approach 2\] Using Bit Masking with Backtracking - O(9^(n\*n)) Time and O(n) Space](https://www.geeksforgeeks.org/dsa/sudoku-backtracking-7/#expected-approach-using-bit-masking-with-backtracking-o9nn-time-and-on-space) ### \[Approach 1\] Using Backtracking > The idea to solve Sudoku is to use backtracking, where we recursively try to fill the empty cells with numbers from 1 to 9. For every unassigned cell, we place a number and then check whether it is valid to place that number in the given row, column, and 3Γ—3 subgrid. If it is valid, we move to the next cell; if not, we backtrack and try another number. This process continues until all cells are filled or no solution exists. C++ `` ``` #include <iostream> ``` ``` #include <vector> ``` ``` using namespace std; ``` ``` ​ ``` ``` // Function to check if it is safe to place num at mat[row][col] ``` ``` bool isSafe(vector<vector<int>> &mat, int row, int col, int num) { ``` ``` ​ ``` ``` // Check if num exist in the row ``` ``` for (int x = 0; x <= 8; x++) ``` ``` if (mat[row][x] == num) ``` ``` return false; ``` ``` ​ ``` ``` // Check if num exist in the col ``` ``` for (int x = 0; x <= 8; x++) ``` ``` if (mat[x][col] == num) ``` ``` return false; ``` ``` ​ ``` ``` // Check if num exist in the 3x3 sub-matrix ``` ``` int startRow = row - (row % 3), startCol = col - (col % 3); ``` ``` ​ ``` ``` for (int i = 0; i < 3; i++) ``` ``` for (int j = 0; j < 3; j++) ``` ``` if (mat[i + startRow][j + startCol] == num) ``` ``` return false; ``` ``` ​ ``` ``` return true; ``` ``` } ``` ``` ​ ``` ``` // Function to solve the Sudoku problem ``` ``` bool solveSudokuRec(vector<vector<int>> &mat, int row, int col) { ``` ``` int n = mat.size(); ``` ``` ​ ``` ``` // base case: Reached nth column of last row ``` ``` if (row == n - 1 && col == n) ``` ``` return true; ``` ``` ​ ``` ``` // If last column of the row go to next row ``` ``` if (col == n) { ``` ``` row++; ``` ``` col = 0; ``` ``` } ``` ``` ​ ``` ``` // If cell is already occupied then move forward ``` ``` if (mat[row][col] != 0) ``` ``` return solveSudokuRec(mat, row, col + 1); ``` ``` ​ ``` ``` for (int num = 1; num <= n; num++) { ``` ``` ​ ``` ``` // If it is safe to place num at current position ``` ``` if (isSafe(mat, row, col, num)) { ``` ``` mat[row][col] = num; ``` ``` if (solveSudokuRec(mat, row, col + 1)) ``` ``` return true; ``` ``` mat[row][col] = 0; ``` ``` } ``` ``` } ``` ``` ``` ``` return false; ``` ``` } ``` ``` ​ ``` ``` void solveSudoku(vector<vector<int>> &mat) { ``` ``` solveSudokuRec(mat, 0, 0); ``` ``` } ``` ``` ​ ``` ``` int main() { ``` ``` vector<vector<int>> mat = { ``` ``` {3, 0, 6, 5, 0, 8, 4, 0, 0}, ``` ``` {5, 2, 0, 0, 0, 0, 0, 0, 0}, ``` ``` {0, 8, 7, 0, 0, 0, 0, 3, 1}, ``` ``` {0, 0, 3, 0, 1, 0, 0, 8, 0}, ``` ``` {9, 0, 0, 8, 6, 3, 0, 0, 5}, ``` ``` {0, 5, 0, 0, 9, 0, 6, 0, 0}, ``` ``` {1, 3, 0, 0, 0, 0, 2, 5, 0}, ``` ``` {0, 0, 0, 0, 0, 0, 0, 7, 4}, ``` ``` {0, 0, 5, 2, 0, 6, 3, 0, 0}}; ``` ``` ​ ``` ``` solveSudoku(mat); ``` ``` ``` ``` for (int i = 0; i < mat.size(); i++) { ``` ``` for (int j = 0; j < mat.size(); j++) ``` ``` cout << mat[i][j] << " "; ``` ``` cout << endl; ``` ``` } ``` ``` ​ ``` ``` return 0; ``` ``` } ``` Java `` ``` // Function to check if it is safe to place num at mat[row][col] ``` Python `` ``` # Function to check if it is safe to place num at mat[row][col] ``` C\# `` ``` // Function to check if it is safe to place num at mat[row][col] ``` JavaScript `` ``` // Function to check if it is safe to place num at mat[row][col] ``` **Output** ``` 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 ``` ****Time complexity:**** O(9(n\*n)), For every unassigned index, there are 9 possible options and for each index ****Auxiliary Space:**** O(1) ### \[Approach 2\] Using Bit Masking with Backtracking - O(9(n\*n)) Time and O(n) Space We can solve this efficiently by using backtracking combined with [bitmasking](https://www.geeksforgeeks.org/dsa/what-is-bitmasking/). The idea is simple: for every empty cell, we attempt to place numbers from 1 to 9 and move recursively to the next cell. > The key optimization lies in avoiding repeated scanning of the row, column, and 3Γ—3 box each time we place a number. Instead, we maintain three bitmask arraysβ€”rows, cols, and boxes. In these arrays, each bit indicates whether a particular number has already been used in that row, column, or box. Before placing a number, we simply check the corresponding bits in O(1) time to determine if it is valid. If it is safe, we set the bits and continue with recursion. If the placement leads to a dead end, we backtrack by unsetting the bits and trying another option. C++ `` ``` #include <iostream> ``` ``` #include <vector> ``` ``` using namespace std; ``` ``` ​ ``` ``` // Function to heck if it is safe to place num at mat[row][col] ``` ``` bool isSafe(vector<vector<int>> &mat, int i, int j, int num, ``` ``` vector<int> &row, vector<int> &col, vector<int> &box) { ``` ``` ``` ``` if( (row[i] & (1 << num)) || (col[j] & (1 << num)) || ``` ``` (box[i / 3 * 3 + j / 3] & (1 << num)) ) ``` ``` return false; ``` ``` ``` ``` return true; ``` ``` } ``` ``` ​ ``` ``` bool sudokuSolverRec(vector<vector<int>> &mat, int i, int j, ``` ``` vector<int> &row, vector<int> &col, vector<int> &box) { ``` ``` int n = mat.size(); ``` ``` ​ ``` ``` // base case: Reached nth column of last row ``` ``` if (i == n - 1 && j == n) ``` ``` return true; ``` ``` ​ ``` ``` // If reached last column of the row go to next row ``` ``` if (j == n) { ``` ``` i++; ``` ``` j = 0; ``` ``` } ``` ``` ``` ``` // If cell is already occupied then move forward ``` ``` if (mat[i][j] != 0) ``` ``` return sudokuSolverRec(mat, i, j + 1, row, col, box); ``` ``` ​ ``` ``` for (int num = 1; num <= n; num++) { ``` ``` ``` ``` // If it is safe to place num at current position ``` ``` if (isSafe(mat, i, j, num, row, col, box)) { ``` ``` mat[i][j] = num; ``` ``` ``` ``` // Update masks for the corresponding row, column and box ``` ``` row[i] |= (1 << num); ``` ``` col[j] |= (1 << num); ``` ``` box[i / 3 * 3 + j / 3] |= (1 << num); ``` ``` ``` ``` if (sudokuSolverRec(mat, i, j + 1, row, col, box)) ``` ``` return true; ``` ``` ``` ``` // Unmask the number num in the corresponding row, column and box masks ``` ``` mat[i][j] = 0; ``` ``` row[i] &= ~(1 << num); ``` ``` col[j] &= ~(1 << num); ``` ``` box[i / 3 * 3 + j / 3] &= ~(1 << num); ``` ``` } ``` ``` } ``` ``` ``` ``` return false; ``` ``` } ``` ``` ​ ``` ``` void solveSudoku(vector<vector<int>> &mat) { ``` ``` int n = mat.size(); ``` ``` vector<int> row(n, 0), col(n, 0), box(n, 0); ``` ``` ​ ``` ``` // Set the bits in bitmasks for values that are initital present ``` ``` for (int i = 0; i < n; i++) { ``` ``` for (int j = 0; j < n; j++) { ``` ``` if (mat[i][j] != 0) { ``` ``` row[i] |= (1 << mat[i][j]); ``` ``` col[j] |= (1 << mat[i][j]); ``` ``` box[ (i / 3) * 3 + j / 3] |= (1 << mat[i][j]); ``` ``` } ``` ``` } ``` ``` } ``` ``` ​ ``` ``` sudokuSolverRec(mat, 0, 0, row, col, box); ``` ``` } ``` ``` ​ ``` ``` int main() { ``` ``` vector<vector<int>> mat = { ``` ``` {3, 0, 6, 5, 0, 8, 4, 0, 0}, ``` ``` {5, 2, 0, 0, 0, 0, 0, 0, 0}, ``` ``` {0, 8, 7, 0, 0, 0, 0, 3, 1}, ``` ``` {0, 0, 3, 0, 1, 0, 0, 8, 0}, ``` ``` {9, 0, 0, 8, 6, 3, 0, 0, 5}, ``` ``` {0, 5, 0, 0, 9, 0, 6, 0, 0}, ``` ``` {1, 3, 0, 0, 0, 0, 2, 5, 0}, ``` ``` {0, 0, 0, 0, 0, 0, 0, 7, 4}, ``` ``` {0, 0, 5, 2, 0, 6, 3, 0, 0}}; ``` ``` ​ ``` ``` solveSudoku(mat); ``` ``` ``` ``` for (int i = 0; i < mat.size(); i++) { ``` ``` for (int j = 0; j < mat.size(); j++) ``` ``` cout << mat[i][j] << " "; ``` ``` cout << endl; ``` ``` } ``` ``` ​ ``` ``` return 0; ``` ``` } ``` Java `` ``` // Unmask the number num in the corresponding row, column and box masks ``` Python `` ``` if (row[i] & (1 << num)) or(col[j] & (1 << num)) or (box[i // 3 * 3 + j // 3] & (1 << num)): ``` C\# `` ``` // Unmask the number num in the corresponding row, column and box masks ``` JavaScript `` ``` // Unmask the number num in the corresponding row, column and box masks ``` **Output** ``` 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 ``` Sudoku (Visualisation) Comment [K](https://www.geeksforgeeks.org/user/kartik/) [kartik](https://www.geeksforgeeks.org/user/kartik/) 183 Article Tags: Article Tags: [Backtracking](https://www.geeksforgeeks.org/category/dsa/algorithm/backtracking/) [Matrix](https://www.geeksforgeeks.org/category/dsa/data-structures/matrix/) [DSA](https://www.geeksforgeeks.org/category/dsa/) [Microsoft](https://www.geeksforgeeks.org/tag/microsoft/) [Amazon](https://www.geeksforgeeks.org/tag/amazon/) [Google](https://www.geeksforgeeks.org/tag/google/) [Oracle](https://www.geeksforgeeks.org/tag/oracle/) [Flipkart](https://www.geeksforgeeks.org/tag/flipkart/) [Directi](https://www.geeksforgeeks.org/tag/directi/) [Zoho](https://www.geeksforgeeks.org/tag/zoho/) [MAQ Software](https://www.geeksforgeeks.org/tag/maq-software/) [MakeMyTrip](https://www.geeksforgeeks.org/tag/makemytrip/) [Ola Cabs](https://www.geeksforgeeks.org/tag/ola-cabs/) [PayPal](https://www.geeksforgeeks.org/tag/paypal/) [\+10 More]() ### Explore [![GeeksforGeeks](https://media.geeksforgeeks.org/auth-dashboard-uploads/gfgFooterLogo.png)](https://www.geeksforgeeks.org/) ![location](https://media.geeksforgeeks.org/img-practice/Location-1685004904.svg) Corporate & Communications Address: A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305) ![location](https://media.geeksforgeeks.org/img-practice/Location-1685004904.svg) Registered Address: K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305 [![GFG App on Play Store](https://media.geeksforgeeks.org/auth-dashboard-uploads/googleplay-%281%29.png)](https://geeksforgeeksapp.page.link/gfg-app)[![GFG App on App Store](https://media.geeksforgeeks.org/auth-dashboard-uploads/appstore-%281%29.png)](https://geeksforgeeksapp.page.link/gfg-app) - Company - [About Us](https://www.geeksforgeeks.org/about/) - [Legal](https://www.geeksforgeeks.org/legal/) - [Privacy Policy](https://www.geeksforgeeks.org/legal/privacy-policy/) - [Contact Us](https://www.geeksforgeeks.org/about/contact-us/) - [Advertise with us](https://www.geeksforgeeks.org/advertise-with-us/) - [GFG Corporate Solution](https://www.geeksforgeeks.org/gfg-corporate-solution/) - [Campus Training Program](https://www.geeksforgeeks.org/campus-training-program/) - Explore - [POTD](https://www.geeksforgeeks.org/problem-of-the-day) - [Job-A-Thon](https://practice.geeksforgeeks.org/events/rec/job-a-thon/) - [Blogs](https://www.geeksforgeeks.org/category/blogs/?type=recent) - [Nation Skill Up](https://www.geeksforgeeks.org/nation-skill-up/) - Tutorials - [Programming Languages](https://www.geeksforgeeks.org/computer-science-fundamentals/programming-language-tutorials/) - [DSA](https://www.geeksforgeeks.org/dsa/dsa-tutorial-learn-data-structures-and-algorithms/) - [Web Technology](https://www.geeksforgeeks.org/web-tech/web-technology/) - [AI, ML & Data Science](https://www.geeksforgeeks.org/machine-learning/ai-ml-and-data-science-tutorial-learn-ai-ml-and-data-science/) - [DevOps](https://www.geeksforgeeks.org/devops/devops-tutorial/) - [CS Core Subjects](https://www.geeksforgeeks.org/gate/gate-exam-tutorial/) - [Interview Preparation](https://www.geeksforgeeks.org/aptitude/interview-corner/) - [Software and Tools](https://www.geeksforgeeks.org/websites-apps/software-and-tools-a-to-z-list/) - Courses - [ML and Data Science](https://www.geeksforgeeks.org/courses/category/machine-learning-data-science) - [DSA and Placements](https://www.geeksforgeeks.org/courses/category/dsa-placements) - [Web Development](https://www.geeksforgeeks.org/courses/category/development-testing) - [Programming Languages](https://www.geeksforgeeks.org/courses/category/programming-languages) - [DevOps & Cloud](https://www.geeksforgeeks.org/courses/category/cloud-devops) - [GATE](https://www.geeksforgeeks.org/courses/category/gate) - [Trending Technologies](https://www.geeksforgeeks.org/courses/category/trending-technologies/) - Videos - [DSA](https://www.geeksforgeeks.org/videos/category/sde-sheet/) - [Python](https://www.geeksforgeeks.org/videos/category/python/) - [Java](https://www.geeksforgeeks.org/videos/category/java-w6y5f4/) - [C++](https://www.geeksforgeeks.org/videos/category/c/) - [Web Development](https://www.geeksforgeeks.org/videos/category/web-development/) - [Data Science](https://www.geeksforgeeks.org/videos/category/data-science/) - [CS Subjects](https://www.geeksforgeeks.org/videos/category/cs-subjects/) - Preparation Corner - [Interview Corner](https://www.geeksforgeeks.org/interview-prep/interview-corner/) - [Aptitude](https://www.geeksforgeeks.org/aptitude/aptitude-questions-and-answers/) - [Puzzles](https://www.geeksforgeeks.org/aptitude/puzzles/) - [GfG 160](https://www.geeksforgeeks.org/courses/gfg-160-series) - [System Design](https://www.geeksforgeeks.org/system-design/system-design-tutorial/) [@GeeksforGeeks, Sanchhaya Education Private Limited](https://www.geeksforgeeks.org/), [All rights reserved](https://www.geeksforgeeks.org/copyright-information/) ![]()
Readable Markdown
``` #include <iostream> ``` ``` #include <vector> ``` ``` using namespace std; ``` ``` ​ ``` ``` ``` ``` bool isSafe(vector<vector<int>> &mat, int i, int j, int num, ``` ``` vector<int> &row, vector<int> &col, vector<int> &box) { ``` ``` ``` ``` if( (row[i] & (1 << num)) || (col[j] & (1 << num)) || ``` ``` (box[i / 3 * 3 + j / 3] & (1 << num)) ) ``` ``` return false; ``` ``` ``` ``` return true; ``` ``` } ``` ``` ​ ``` ``` bool sudokuSolverRec(vector<vector<int>> &mat, int i, int j, ``` ``` vector<int> &row, vector<int> &col, vector<int> &box) { ``` ``` int n = mat.size(); ``` ``` ​ ``` ``` ``` ``` if (i == n - 1 && j == n) ``` ``` return true; ``` ``` ​ ``` ``` ``` ``` if (j == n) { ``` ``` i++; ``` ``` j = 0; ``` ``` } ``` ``` ``` ``` ``` ``` if (mat[i][j] != 0) ``` ``` return sudokuSolverRec(mat, i, j + 1, row, col, box); ``` ``` ​ ``` ``` for (int num = 1; num <= n; num++) { ``` ``` ``` ``` ``` ``` if (isSafe(mat, i, j, num, row, col, box)) { ``` ``` mat[i][j] = num; ``` ``` ``` ``` ``` ``` row[i] |= (1 << num); ``` ``` col[j] |= (1 << num); ``` ``` box[i / 3 * 3 + j / 3] |= (1 << num); ``` ``` ``` ``` if (sudokuSolverRec(mat, i, j + 1, row, col, box)) ``` ``` return true; ``` ``` ``` ``` ``` ``` mat[i][j] = 0; ``` ``` row[i] &= ~(1 << num); ``` ``` col[j] &= ~(1 << num); ``` ``` box[i / 3 * 3 + j / 3] &= ~(1 << num); ``` ``` } ``` ``` } ``` ``` ``` ``` return false; ``` ``` } ``` ``` ​ ``` ``` void solveSudoku(vector<vector<int>> &mat) { ``` ``` int n = mat.size(); ``` ``` vector<int> row(n, 0), col(n, 0), box(n, 0); ``` ``` ​ ``` ``` ``` ``` for (int i = 0; i < n; i++) { ``` ``` for (int j = 0; j < n; j++) { ``` ``` if (mat[i][j] != 0) { ``` ``` row[i] |= (1 << mat[i][j]); ``` ``` col[j] |= (1 << mat[i][j]); ``` ``` box[ (i / 3) * 3 + j / 3] |= (1 << mat[i][j]); ``` ``` } ``` ``` } ``` ``` } ``` ``` ​ ``` ``` sudokuSolverRec(mat, 0, 0, row, col, box); ``` ``` } ``` ``` ​ ``` ``` int main() { ``` ``` vector<vector<int>> mat = { ``` ``` {3, 0, 6, 5, 0, 8, 4, 0, 0}, ``` ``` {5, 2, 0, 0, 0, 0, 0, 0, 0}, ``` ``` {0, 8, 7, 0, 0, 0, 0, 3, 1}, ``` ``` {0, 0, 3, 0, 1, 0, 0, 8, 0}, ``` ``` {9, 0, 0, 8, 6, 3, 0, 0, 5}, ``` ``` {0, 5, 0, 0, 9, 0, 6, 0, 0}, ``` ``` {1, 3, 0, 0, 0, 0, 2, 5, 0}, ``` ``` {0, 0, 0, 0, 0, 0, 0, 7, 4}, ``` ``` {0, 0, 5, 2, 0, 6, 3, 0, 0}}; ``` ``` ​ ``` ``` solveSudoku(mat); ``` ``` ``` ``` for (int i = 0; i < mat.size(); i++) { ``` ``` for (int j = 0; j < mat.size(); j++) ``` ``` cout << mat[i][j] << " "; ``` ``` cout << endl; ``` ``` } ``` ``` ​ ``` ``` return 0; ``` ``` } ```
Shard103 (laksa)
Root Hash12046344915360636903
Unparsed URLorg,geeksforgeeks!www,/dsa/sudoku-backtracking-7/ s443