回溯:
在递归中暴力穷举,算法复杂度
难点
- 状态回溯
- 终止条件
- 剪枝
一般的回溯问题
字符串问题
例题:No.17 Letter Combinations of a Phone Numbers
LeetCode练习题
- No.93 Restore IP Addresses
- No.131 Palindrome Parititioning
排列问题
例题:No.46 Permutation
|
|
以前的解
LeetCode练习题
- No.47 Permutations II
组合问题
例题 No.77 Combinations
|
|
剪枝优化之后
LeetCode练习题
- No.39 Combination Sum
- No.40 Combination Sum II
- No.216 Combination Sum III
- No.78 Subsets
- No.90 Subsets II
- No.401 Binary Watch
二维平面上的回溯
二维平面上回溯的一般做法
例题: No.79 Word Search
floodfill算法(深度优先)
例题:No.200 Number of Islands
和普通回溯相比,没有状态回溯的过程
LeetCode练习题
No.130 Surrounded Regions
此题的神奇之处在于,如果用辅助数组来保存访问状态的话,会超时,也就是被注释掉的
used
数组;借助自身值来表示状态可以解决这个问题。12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061class Solution {public:int m, n;int d[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};bool inArea(int x, int y) {return (x >= 0 && y >= 0 && x < m && y < n);}void func(vector<vector<char>>& board, int pos_x, int pos_y/*, vector<vector<bool>>& used*/) {//used[pos_x][pos_y] = true;board[pos_x][pos_y] = '*';for (int i = 0; i < 4; i++) {int x = pos_x + d[i][0];int y = pos_y + d[i][1];//if (inArea(x, y) && board[x][y] == 'O' && used[x][y] == false) {if (inArea(x, y) && board[x][y] == 'O') {func(board, x, y);}}}void solve(vector<vector<char>>& board) {if (board.empty()) return;m = board.size();n = board[0].size();vector<vector<bool>> used(m, vector<bool>(n, false));for (int i = 0; i < m; i ++) {int j = 0;if (board[i][j] == 'O') {func(board, i, j);}j = n - 1;if (board[i][j] == 'O'){func(board, i, j);}}for (int j = 1; j < n - 1; j++) {int i = 0;if (board[i][j] == 'O') {func(board, i, j);}i = m - 1;if (board[i][j] == 'O') {func(board, i, j);}}for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {if (board[i][j] == 'O') {board[i][j] = 'X';}if (board[i][j] == '*') {board[i][j] = 'O';}}}}};No.417 Pacific Atlantic Water Flow
人工智能类应用
例题: No.51 N-Queens
|
|
LeetCode练习题
- Sudoku Solver
被京东的面试虐过
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495using namespace std;vector<vector<char>> res;template<typename T>void print2DBoard(vector<vector<T>> board) {int i = 0;int j = 0;for (auto v1 : board) {for (auto v2 : v1) {cout << v2 << ' ';if (++j % 3 == 0)cout << "| ";}j = 0;cout << endl;if (++i % 3 == 0)cout << "-----------------------" << endl;;}cout << endl;}template<typename T>void print2DVector(vector<vector<T>> board) {for (auto v1 : board) {for (auto v2 : v1) {cout << v2 << '\t';}cout << endl << endl;}cout << endl;}void func(int i, int j, vector<vector<bool>>& row, vector<vector<bool>>& col, vector<vector<bool>>& grid, vector<vector<char>>& board) {if (i == 9) {res = board;return;}if (board[i][j] == '.') {for (int k = 0; k < 9; k++) {if (row[i][k] == false && col[j][k] == false && grid[(i / 3) * 3 + j / 3][k] == false) {board[i][j] = k + '1';row[i][k] = true;col[j][k] = true;grid[(i / 3) * 3 + j / 3][k] = true;func((i + (j + 1) / 9), (j + 1) % 9, row, col, grid, board);grid[(i / 3) * 3 + j / 3][k] = false;col[j][k] = false;row[i][k] = false;board[i][j] = '.';}}}else {func((i + (j + 1) / 9), (j + 1) % 9, row, col, grid, board);}}void solveSudoku(vector<vector<char>>& board) {vector<vector<bool>> row(9, vector<bool>(9, false));vector<vector<bool>> col(9, vector<bool>(9, false));vector<vector<bool>> grid(9, vector<bool>(9, false));for (int i = 0; i < 9; i++) {for (int j = 0; j < 9; j++) {if (board[i][j] != '.') {int num = board[i][j] - '0';row[i][num - 1] = true;col[j][num - 1] = true;grid[(i / 3) * 3 + j / 3][num - 1] = true;}}}//cout << "print row" << endl << "-----------------------" << endl;//print2DVector(row);//cout << "print col" << endl << "-----------------------" << endl;//print2DVector(col);//cout << "print grid" << endl << "-----------------------" << endl;//print2DVector(grid);func(0, 0, row, col, grid, board);board = res;//print2DBoard(board);}int main() {vector<vector<char>> input;string str[9] = { "..9748...", "7........", ".2.1.9...", "..7...24.", ".64.1.59.", ".98...3..", "...8.3.2.", "........6", "...2759.." };for (auto s : str) {input.push_back(vector<char>(s.begin(), s.end()));}print2DBoard(input);solveSudoku(input);print2DBoard(res);return 0;}