算法训练Day30:332.重新安排行程 51. N皇后 37. 解数独

文章目录

  • 重新安排行程
    • 题解
  • [N 皇后](https://leetcode.cn/problems/n-queens/description/)
    • 题解
  • 解数独
    • 题解

重新安排行程

Category Difficulty Likes Dislikes ContestSlug ProblemIndex Score
algorithms Hard (47.57%) 765 0 - - 0
Tags

Companies

给你一份航线列表 tickets ,其中 tickets[i] = [fromi, toi] 表示飞机出发和降落的机场地点。请你对该行程进行重新规划排序。

所有这些机票都属于一个从 JFK(肯尼迪国际机场)出发的先生,所以该行程必须从 JFK 开始。如果存在多种有效的行程,请你按字典排序返回最小的行程组合。

  • 例如,行程 ["JFK", "LGA"]["JFK", "LGB"] 相比就更小,排序更靠前。

假定所有机票至少存在一种合理的行程。且所有的机票 必须都用一次 且 只能用一次。

示例 1:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2dGpsxNP-1681370461727)(null)]

输入:tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
输出:["JFK","MUC","LHR","SFO","SJC"]

示例 2:

算法训练Day30:332.重新安排行程 51. N皇后 37. 解数独_第1张图片

输入: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
  • fromitoi 由大写英文字母组成
  • fromi != toi

Discussion | Solution

题解

// @lc code=start
class Solution {
private:
    unordered_map> targets;
    bool backtracking(int ticketNum, vector& result) {
        if(result.size() == ticketNum + 1) {
            return true;
        }
        for(pair& 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 findItinerary(vector>& tickets) {
        targets.clear();
        vector result;
        for(const vector& vec : tickets) {
            targets[vec[0]][vec[1]]++;
        }
        result.push_back("JFK");
        backtracking(tickets.size(),result);
        return result;
    }
};

[参考文章](代码随想录 (programmercarl.com))

N 皇后

Category Difficulty Likes Dislikes ContestSlug ProblemIndex Score
algorithms Hard (74.17%) 1726 0 - - 0
Tags

Companies

按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。

n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。

给你一个整数 n ,返回所有不同的 n 皇后问题 的解决方案。

每一种解法包含一个不同的 n 皇后问题 的棋子放置方案,该方案中 'Q''.' 分别代表了皇后和空位。

示例 1:

算法训练Day30:332.重新安排行程 51. N皇后 37. 解数独_第2张图片

输入:n = 4
输出:[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
解释:如上图所示,4 皇后问题存在两个不同的解法。

示例 2:

输入:n = 1
输出:[["Q"]]

提示:

  • 1 <= n <= 9

Discussion | Solution

题解

// @lc code=start
class Solution {
private:
    vector> result;

    void backtracking(int n,int row,vector& 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& chessboard, int n) {

        for(int i = 0; i < row; ++i) {
            if(chessboard[i][col] == 'Q') {
                return false;
            }
        }

        for(int i = row - 1, j = col - 1; i >= 0 && j >= 0 ; i--,j--) {
            if(chessboard[i][j] == 'Q') {
                return false;
            }
        }

        for(int i = row - 1, j = col + 1; i >= 0 && j >=0; i--, j++) {
            if(chessboard[i][j] == 'Q') {
                return false;
            }
        }
        return true;
        }
public:
    vector> solveNQueens(int n) {
        result.clear();
        vector chessboard(n,string(n,'.'));
        backtracking(n,0,chessboard);
        return result;
    }
};

[参考文章](代码随想录 (programmercarl.com))

解数独

Category Difficulty Likes Dislikes ContestSlug ProblemIndex Score
algorithms Hard (67.60%) 1596 0 - - 0
Tags

数组 | 回溯 | 矩阵

Companies

编写一个程序,通过填充空格来解决数独问题。

数独的解法需 遵循如下规则

  1. 数字 1-9 在每一行只能出现一次。
  2. 数字 1-9 在每一列只能出现一次。
  3. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)

数独部分空格内已填入了数字,空白格用 '.' 表示。

示例 1:

算法训练Day30:332.重新安排行程 51. N皇后 37. 解数独_第3张图片

输入: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"]]
解释:输入的数独如上图所示,唯一有效的解决方案如下所示:

提示:

  • board.length == 9
  • board[i].length == 9
  • board[i][j] 是一位数字或者 '.'
  • 题目数据 保证 输入数独仅有一个解

Discussion | Solution

题解

// @lc code=start
class Solution {
private:
    bool backtracking(vector>& 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++) {
                        if(isValid(i,j,k,board)) {
                            board[i][j] = k;
                            if(backtracking(board)) return true;
                            board[i][j] = '.';
                        }
                    }
                    return false;
                }
            }
        }
        return true;
    } 

    bool isValid(int row,int col,char val,vector>& 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++) {
            for(int j = startCol; j < startCol + 3; j++) {
                if(board[i][j] == val) {
                    return false;
                }
            }
        }
        return true;
    }
public:
    void solveSudoku(vector>& board) {
        backtracking(board);
    }
};

startCol; j < startCol + 3; j++) {
if(board[i][j] == val) {
return false;
}
}
}
return true;
}
public:
void solveSudoku(vector& board) {
backtracking(board);
}
};


[参考文章]([代码随想录 (programmercarl.com)](https://programmercarl.com/0037.解数独.html#总结))

你可能感兴趣的:(算法训练,回溯算法,算法,leetcode,职场和发展)