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

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

  • 332.重新安排行程
  • 51. N皇后
  • 37. 解数独

332.重新安排行程

题目链接
文章讲解

class Solution {
public:
    unordered_map<string, map<string, int>> ticketsMap;
    vector<string> ans = {"JFK"};

    bool backtracking(const int& ticketNums) {
        if (ans.size() == ticketNums + 1) return true;
        for (pair<const string, int>& ticket : ticketsMap[ans.back()]) {
            if (ticket.second > 0) {
                ticket.second--;
                ans.push_back(ticket.first);
                if (backtracking(ticketNums)) return true;
                ans.pop_back();
                ticket.second++;
            }
        }
        return false;
    }

    vector<string> findItinerary(vector<vector<string>>& tickets) {
        for (vector<string>& ticket : tickets)
            ticketsMap[ticket[0]][ticket[1]]++;
        backtracking(tickets.size());
        return ans;
    }
};

51. N皇后

题目链接
文章讲解

class Solution {
public:
    vector<vector<string>> ans;

    bool isVaild(const int& row, const int& col, const int& n, const vector<string>& path) {
        for (int i = 0; i < row; i++) {
            if (path[i][col] == 'Q')
                return false;
        }
        for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
            if (path[i][j] == 'Q')
                return false;
        }
        for (int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) {
            if (path[i][j] == 'Q')
                return false;
        }
        return true;
    }
    
    void backtracking(int row, vector<string>& path, const int& n) {
        if (row == n) {
            ans.push_back(path);
            return;
        }
        for (int col = 0; col < n; col++) {
            if (isVaild(row, col, n, path)) {
                path[row][col] = 'Q';
                backtracking(row + 1, path, n);
                path[row][col] = '.';
            }
        }
    }

    vector<vector<string>> solveNQueens(int n) {
        vector<string> path(n, string(n, '.'));
        backtracking(0, path, n);
        return ans;
    }
};

37. 解数独

题目链接
文章讲解

class Solution {
public:
    bool isVaild(const int& row, const int& col, char n, vector<vector<char>>& board) {
        for (int i = 0; i < 9; i++) {
            if (board[i][col] == n)
                return false;
        }
        for (int i = 0; i < 9; i++) {
            if (board[row][i] == n)
                return false;
        }
        int startRow = (row / 3) * 3, startCol = (col / 3) * 3;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[startRow + i][startCol + j] == n)
                    return false;
            }
        }
        return true;
    }

    bool backtracking(vector<vector<char>>& board) {
        for (int i = 0; i < board.size(); i++) {
            for (int j = 0; j < board[i].size(); j++) {
                if (board[i][j] != '.') continue;
                for (char n = '1'; n <= '9'; n++) {
                    if (isVaild(i, j, n, board)) {
                        board[i][j] = n;
                        if (backtracking(board)) return true;
                        board[i][j] = '.';
                    }
                }
                return false;
            }
        }
        return true;
    }

    void solveSudoku(vector<vector<char>>& board) {
        backtracking(board);
    }
};

你可能感兴趣的:(算法)