0% found this document useful (0 votes)
3 views2 pages

Backtracking

The document provides a detailed explanation of backtracking algorithms, including a template for implementing backtracking in C++ for generating combinations and solving Sudoku puzzles. It outlines the structure of the backtracking function, conditions for safe paths, and the recursive nature of the algorithm. Additionally, it includes a specific implementation for a Sudoku solver that checks for valid placements of numbers on the board.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Backtracking

The document provides a detailed explanation of backtracking algorithms, including a template for implementing backtracking in C++ for generating combinations and solving Sudoku puzzles. It outlines the structure of the backtracking function, conditions for safe paths, and the recursive nature of the algorithm. Additionally, it includes a specific implementation for a Sudoku solver that checks for valid placements of numbers on the board.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Backtracking

Start Point
While (More possibilities remain)
For each path from start point
Check if it is a safe path
|— If Yes, select it, Recursive calls to rest paths
|— Undo this move
|— If No, continue.

Template

void backtrack(vector<vector<int>> list, vector<int> tempList, vector<int> nums, int start){


// Use conditions as per required for example
//
// if(tempList.size() == k) {
// list.push_back(tempList);
// return;
// }
//
// if(tempList.size() == nums.size())
list.push_back(tempList);

for(int i=start // or 0; i<nums.size(); I++){


// Use if condition to check if it safe path for example
// if(i>start && nums[i] == nums[i-1]) continue;
// if(used[i]) continue;
// if(!used[i] && i>0 && nums[i] == nums[i+1]) continue;

tempList.push_back(nums[i]);
// used[i] = true;
backtrack(list, tempList, nums, i+1);
// used[i] = false;
tempList.pop_back();
}
}

vector<vector<int>> func(vector<int> nums){


vector<vector<int>> list;
vector<int> tempList;
// vector<bool> used(nums.size, false);

sort(nuts.begin(), nuts.end());
backtrack(list, tempList, nums, 0);
}
Backtracking
Sudoku Solver
class Solution {
public:
bool isVal(vector<vector<char>>& board, int row, int col, int guess) {
char ch = '0' + guess;

for (int i = 0; i < 9; i++) {


if (board[i][col] == ch) return false;
}

for (int i = 0; i < 9; i++) {


if (board[row][i] == ch) return false;
}

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
int r = (row / 3) * 3 + i;
int c = (col / 3) * 3 + j;
if (board[r][c] == ch) return false;
}
}
return true;
}

bool backtrack(vector<vector<char>>& board, int row, int col){


for(int i = (row*9)+col; i<81; i++){
if(board[i/9][i%9] != '.') continue;

for(int guess=1; guess<10; guess++){


if(isVal(board, i/9, i%9, guess)){
board[i/9][i%9] = '0' + guess;

int j = i+1;
if (backtrack(board, j/9, j%9)) return true;
board[i/9][i%9] = '.';
}
}
return false;
}
return true;
}

void solveSudoku(vector<vector<char>>& board) {


backtrack(board, 0, 0);
return;
}
};

You might also like