Day29 休息~
一、参考资料
重点!! 回溯算法总结篇
https://programmercarl.com/%E5%9B%9E%E6%BA%AF%E6%80%BB%E7%BB%93.html
组合问题:N个数里面按一定规则找出k个数的集合
排列问题:N个数按一定规则全排列,有几种排列方式
切割问题:一个字符串按一定规则有几种切割方式
子集问题:一个N个数的集合里有多少符合条件的子集
棋盘问题:N皇后,解数独等等
重新安排行程
https://programmercarl.com/0332.%E9%87%8D%E6%96%B0%E5%AE%89%E6%8E%92%E8%A1%8C%E7%A8%8B.html
N皇后
https://programmercarl.com/0051.N%E7%9A%87%E5%90%8E.html
视频讲解:https://www.bilibili.com/video/BV1Rd4y1c7Bq
解数独
https://programmercarl.com/0037.%E8%A7%A3%E6%95%B0%E7%8B%AC.html
视频讲解:https://www.bilibili.com/video/BV1TW4y1471V
二、LeetCode332.重新安排行程
给你一份航线列表 tickets ,其中 tickets[i] = [fromi, toi] 表示飞机出发和降落的机场地点。请你对该行程进行重新规划排序。
所有这些机票都属于一个从 JFK(肯尼迪国际机场)出发的先生,所以该行程必须从 JFK 开始。如果存在多种有效的行程,请你按字典排序返回最小的行程组合。
例如,行程 ["JFK", "LGA"] 与 ["JFK", "LGB"] 相比就更小,排序更靠前。
假定所有机票至少存在一种合理的行程。且所有的机票 必须都用一次 且 只能用一次。
示例一:
输入:tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]] 输出:["JFK","MUC","LHR","SFO","SJC"]
示例二:
输入:tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]] 输出:["JFK","ATL","JFK","SFO","ATL","SFO"] 解释:另一种有效的行程是 ["JFK","SFO","ATL","JFK","ATL","SFO"] ,但是它字典排序更大更靠后。
提示:
1 <= tickets.length <= 300
tickets[i].length == 2
fromi.length == 3
toi.length == 3
fromi 和 toi 由大写英文字母组成
fromi != toi
本题以输入:[["JFK", "KUL"], ["JFK", "NRT"], ["NRT", "JFK"]为例,抽象为树形结构如下:
- class Solution {
- private:
- // unordered_map<出发机场, map<到达机场, 航班次数>> targets
- unordered_map<string, map<string, int>> targets;
- bool backtracking(int ticketNum, vector<string>& result) {
- if (result.size() == ticketNum + 1) {
- return true;
- }
- for (pair<const string, int>& target : targets[result[result.size() - 1]]) {
- if (target.second > 0 ) { // 记录到达机场是否飞过了
- result.push_back(target.first);
- target.second--;
- if (backtracking(ticketNum, result)) return true;
- result.pop_back();
- target.second++;
- }
- }
- return false;
- }
- public:
- vector<string> findItinerary(vector<vector<string>>& tickets) {
- targets.clear();
- vector<string> result;
- for (const vector<string>& vec : tickets) {
- targets[vec[0]][vec[1]]++; // 记录映射关系
- }
- result.push_back("JFK"); // 起始机场
- backtracking(tickets.size(), result);
- return result;
- }
- };
三、LeetCode51. N皇后
https://leetcode.cn/problems/n-queens/description/
按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。
n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
给你一个整数 n ,返回所有不同的 n 皇后问题 的解决方案。
每一种解法包含一个不同的 n 皇后问题 的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。
示例一:
输入:n = 4 输出:[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]] 解释:如上图所示,4 皇后问题存在两个不同的解法。
示例二:
输入:n = 1 输出:[["Q"]]
提示:
1 <= n <= 9
这题通过看讲解理解了思路,不过我暂时还不能自己独立写出来~
- class Solution {
- private:
- vector<vector<string>> result;
- // n 为输入的棋盘大小
- // row 是当前递归到棋盘的第几行了
- void backtracking(int n, int row, vector<string>& chessboard) {
- if (row == n) {
- result.push_back(chessboard);
- return;
- }
- for (int col = 0; col < n; col++) {
- if (isValid(row, col, chessboard, n)) { // 验证合法就可以放
- chessboard[row][col] = 'Q'; // 放置皇后
- backtracking(n, row + 1, chessboard);
- chessboard[row][col] = '.'; // 回溯,撤销皇后
- }
- }
- }
- bool isValid(int row, int col, vector<string>& chessboard, int n) {
- // 检查列
- for (int i = 0; i < row; i++) { // 这是一个剪枝
- if (chessboard[i][col] == 'Q') {
- return false;
- }
- }
- // 检查 45度角是否有皇后
- for (int i = row - 1, j = col - 1; i >=0 && j >= 0; i--, j--) {
- if (chessboard[i][j] == 'Q') {
- return false;
- }
- }
- // 检查 135度角是否有皇后
- for(int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) {
- if (chessboard[i][j] == 'Q') {
- return false;
- }
- }
- return true;
- }
- public:
- vector<vector<string>> solveNQueens(int n) {
- result.clear();
- // std::vector<std::string> chessboard(n, std::string(n, '.'));
- vector<string> chessboard(n, string(n, '.'));
- backtracking(n, 0, chessboard);
- return result;
- }
- };
四、LeetCode37. 解数独
https://leetcode.cn/problems/sudoku-solver/description/
编写一个程序,通过填充空格来解决数独问题。
数独的解法需 遵循如下规则:
数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
数独部分空格内已填入了数字,空白格用 '.' 表示。
示例:
输入:board = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]] 输出:[["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]]
只能说,这题更难了
码住卡哥的题解,回头填坑叭:
- class Solution {
- private:
- bool backtracking(vector<vector<char>>& board) {
- for (int i = 0; i < board.size(); i++) { // 遍历行
- for (int j = 0; j < board[0].size(); j++) { // 遍历列
- if (board[i][j] == '.') {
- for (char k = '1'; k <= '9'; k++) { // (i, j) 这个位置放k是否合适
- if (isValid(i, j, k, board)) {
- board[i][j] = k; // 放置k
- if (backtracking(board)) return true; // 如果找到合适一组立刻返回
- board[i][j] = '.'; // 回溯,撤销k
- }
- }
- return false; // 9个数都试完了,都不行,那么就返回false
- }
- }
- }
- return true; // 遍历完没有返回false,说明找到了合适棋盘位置了
- }
- bool isValid(int row, int col, char val, vector<vector<char>>& board) {
- for (int i = 0; i < 9; i++) { // 判断行里是否重复
- if (board[row][i] == val) {
- return false;
- }
- }
- for (int j = 0; j < 9; j++) { // 判断列里是否重复
- if (board[j][col] == val) {
- return false;
- }
- }
- int startRow = (row / 3) * 3;
- int startCol = (col / 3) * 3;
- for (int i = startRow; i < startRow + 3; i++) { // 判断9方格里是否重复
- for (int j = startCol; j < startCol + 3; j++) {
- if (board[i][j] == val ) {
- return false;
- }
- }
- }
- return true;
- }
- public:
- void solveSudoku(vector<vector<char>>& board) {
- backtracking(board);
- }
- };
今日总结:
剩下的回溯专题的“尾巴”,实在太难了,我只做到了理解思路,剩下的就慢慢消化啦
继续加油哈小赵~
标签:JFK,return,int,随想录,51,board,vector,37,result From: https://www.cnblogs.com/ucaszym/p/17128783.html