代码随想录算法训练营Day30|332.重新安排行程、51. N皇后、37. 解数独

332.重新安排行程

题目链接:332.重新安排行程
文档链接:332.重新安排行程

C++实现

class Solution {
private:
    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;
                target.second++;
                result.pop_back();
            }
        }
        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;
    }
};

51. N皇后

题目链接:51. N皇后
文档链接:51. N皇后
视频链接:这就是传说中的N皇后? 回溯算法安排!| LeetCode:51.N皇后

C++实现

class Solution {
private:
    vector<vector<string>> result;
    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;
            }
        }
        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 < n; i--, j++) {
            if (chessboard[i][j] == 'Q') {
                return false;
            }
        }
        return true;
    }

    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] = '.';
            }
        }
    }

public:
    vector<vector<string>> solveNQueens(int n) {
        result.clear();
        vector<string> chessboard(n, string(n,'.'));
        backtracking(n, 0, chessboard);
        return result;
    }
};

37. 解数独

题目链接:37. 解数独
文档链接:37. 解数独
视频链接:回溯算法二维递归?解数独不过如此!| LeetCode:37. 解数独

C++实现

class Solution {
private:
    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 i = 0; i<9;i++){
            if(board[i][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;
    }

      bool backtarcking(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(int k = '1'; k<='9';k++){
                          if(isValid(i,j,k,board)){
                              board[i][j] = k;
                              if(backtarcking(board)) return true;
                              board[i][j] = '.';
                          }
                      }
                      return false;
                  }
              }
          }
          return true;
      }

public:
    void solveSudoku(vector<vector<char>>& board) {
        backtarcking(board);
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值