332. 重新安排行程

 1 class Solution {
 2 public:
 3     unordered_map<string,map<string,int>> targets;
 4     bool back_tracking(int ticketCount, vector<string>& result){
 5         if(result.size() == ticketCount + 1){
 6             return true;
 7         }
 8         // 当前最后一个机场对应的下一个机场和个数.
 9         for(pair<const string,int>& target : targets[result[result.size()-1]]){
10             if(target.second == 0)  continue;   // 已经飞过了
11             target.second--;
12             result.push_back(target.first);
13             if(back_tracking(ticketCount, result))  return true;
14             result.pop_back();
15             target.second++;
16         }
17         return false;
18     }
19 
20     vector<string> findItinerary(vector<vector<string>>& tickets) {
21         vector<string> result;
22         for(vector<string>& vec : tickets){
23             targets[vec[0]][vec[1]]++;
24         }
25         result.push_back("JFK");
26         back_tracking(tickets.size(),result);
27         return result;
28     }
29 };

 

51. N 皇后

 1 class Solution {
 2 public:
 3     vector<vector<string>> result;
 4     // 第r行
 5     void back_tracking(int n, vector<string>& vec, int r){
 6         if(r == n){
 7             result.push_back(vec);
 8             return;
 9         }
10         for(int i = 0; i < n; i++){
11             if(canSetQ(r, i, vec, n)){
12                 vec[r][i] = 'Q';
13                 back_tracking(n, vec, r+1);
14                 vec[r][i] = '.';
15             }
16         }
17     }
18 
19     bool canSetQ(int x, int y,const vector<string>& vec, int n){
20         for(int i = 0; i < n; i++){
21             if(vec[i][y] == 'Q')    return false;
22         }
23         for(int j = 0; j < n; j++){
24             if(vec[x][j] == 'Q')    return false;
25         }
26         for(int i = x,j = y; i <n && j < n && i >= 0 && j >= 0; i++,j++){
27             if(vec[i][j] == 'Q')    return false;
28         }
29         for(int i = x,j = y; i <n && j < n && i >= 0 && j >= 0; i--,j++){
30             if(vec[i][j] == 'Q')    return false;
31         }
32         for(int i = x,j = y; i <n && j < n && i >= 0 && j >= 0; i++,j--){
33             if(vec[i][j] == 'Q')    return false;
34         }
35         for(int i = x,j = y; i <n && j < n && i >= 0 && j >= 0; i--,j--){
36             if(vec[i][j] == 'Q')    return false;
37         }
38         return true;
39     }
40     vector<vector<string>> solveNQueens(int n) {
41         vector<string> vec;
42         for(int i = 0; i < n; i++){
43             string s(n,'.');
44             vec.push_back(s);
45         }
46         back_tracking(n, vec, 0);
47         return result;
48     }
49 };

 37. 解数独

 1 class Solution {
 2 public:
 3     bool back_tracking(vector<vector<char>>& board){
 4         for(int i = 0; i < 9; i++){
 5             for(int j = 0; j < 9; j++){
 6                 if(board[i][j] != '.')  continue;
 7                 for(char k = '1'; k <= '9'; k++){
 8                     if(validate(board, i, j, k)){
 9                         board[i][j] = k;
10                         if(back_tracking(board)) return true;
11                         board[i][j] = '.';
12                     }
13                 }
14                 return false;
15             }
16         }
17         return true;
18     }
19 
20     bool validate(vector<vector<char>>& board, int x, int y, char c){
21         //
22         for(int i = 0; i < 9; i++){
23             if(board[i][y] == c)    return false;
24         }
25         //
26         for(int i = 0; i < 9; i++){
27             if(board[x][i] == c)    return false;
28         }
29         // 小方格
30         for(int i = 0; i < 3; i++){
31             for(int j = 0; j < 3; j++){
32                 if(board[x/3 * 3 + i][y/3 * 3 + j] == c)    return false;
33             }
34         }
35 
36         return true;
37     }
38 
39     void solveSudoku(vector<vector<char>>& board) {
40         back_tracking(board);
41     }
42 };