1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
//! N-Queens II [leetcode: n_queens_II](https://leetcode.com/problems/n-queens-ii/) //! //! The *n*-queens puzzle is the problem of placing *n* queens on an *n×n* chessboard such that no two queens attack each other. //! //! <div> //! <img alt="" src="https://assets.leetcode.com/uploads/2018/10/12/8-queens.png" style="width: 258px; height: 276px;"> //! </div> //! //! Given an integer *n*, return the number of distinct solutions to the n-queens puzzle. //! //! ***Example:*** //! //! ``` //! Input: 4 //! Output: 2 //! Explanation: There are two distinct solutions to the 4-queens puzzle as shown below. //! [ //! [".Q..", // Solution 1 //! "...Q", //! "Q...", //! "..Q."], //! //! ["..Q.", // Solution 2 //! "Q...", //! "...Q", //! ".Q.."] //! ] //! ``` //! /// # Solutions /// /// # Approach 1: DFS /// /// * Time complexity: /// /// * Space complexity: /// /// * Runtime: 4 ms /// * Memory: 2.8 MB /// /// ```rust /// impl Solution { /// pub fn total_n_queens(n: i32) -> i32 { /// if n < 1 { return 0; } /// /// let mut result = 0; /// let mut cols = vec![]; /// let mut xy_sum = vec![]; /// let mut xy_sub = vec![]; /// let row = 0; /// /// Self::_dfs(n, &mut result, row, &mut cols, &mut xy_sum, &mut xy_sub); /// /// result /// } /// /// pub fn _dfs(n: i32, result: &mut i32, row: i32, cols: &mut Vec<i32>, xy_sum: &mut Vec<i32>, xy_sub: &mut Vec<i32>) { /// if row >= n { /// *result += 1; /// return; /// } /// /// for col in 0..n { /// if cols.contains(&col) || xy_sum.contains(&(row + col)) || xy_sub.contains(&(row - col)) { /// continue; /// } /// /// cols.push(col); /// xy_sum.push(row + col); /// xy_sub.push(row - col); /// Self::_dfs(n, result, row + 1, cols, xy_sum, xy_sub); /// /// cols.retain(|&x| x != col); /// xy_sum.retain(|&x| x != (row + col)); /// xy_sub.retain(|&x| x != (row - col)); /// } /// } /// } /// ``` /// /// # Approach 2: DFS /// /// * Time complexity: /// /// * Space complexity: /// /// * Runtime: 0 ms /// * Memory: 2.8 MB /// /// ```rust /// impl Solution { /// pub fn total_n_queens(n: i32) -> Vec<Vec<String>> { /// let mut board = vec![vec!['.'; n as usize]; n as usize]; /// let mut num = 0; /// Self::schedule_queens(&mut board, &mut num, n as usize, 0); /// solution /// } /// /// fn schedule_queens(board: &mut Vec<Vec<char>>, num &mut i32, len: usize, row: usize) { /// for col in 0..len { /// if !Self::collision(&board, len, row, col) { /// board[row][col] = 'Q'; /// if row == len - 1 { /// *num += 1; /// } else { /// Self::schedule_queens(board, solution, len, row+1); /// } /// board[row][col] = '.'; /// } /// } /// } /// /// #[inline(always)] /// fn collision(board: &Vec<Vec<char>>, len: usize, row: usize, col: usize) -> bool { /// for i in 0..row { /// if board[i][col] == 'Q' { return true } /// } /// let (mut i, mut j) = (row as i32 - 1, col as i32 - 1); /// while i >= 0 && j >= 0 { /// if board[i as usize][j as usize] == 'Q' { return true } /// i -= 1; j -= 1; /// } /// let (mut i, mut j) = (row as i32 - 1, col as i32 + 1); /// while i >= 0 && j < len as i32 { /// if board[i as usize][j as usize] == 'Q' { return true} /// i -= 1; j += 1; /// } /// false /// } /// } /// ``` /// /// # Approach 3: BitWise /// /// * Time complexity: /// /// * Space complexity: /// /// * Runtime: 0 ms /// * Memory: 2.3 MB /// /// ```rust /// impl Solution { /// pub fn total_n_queens(n: i32) -> i32 { /// if n < 1 { return 0; } /// /// let mut result = 0; /// Self::_dfs(n, &mut result, 0, 0, 0, 0); /// result /// } /// /// pub fn _dfs(n: i32, result: &mut i32, row: i32, col: i32, pie: i32, na: i32) { /// if row >= n { /// *result += 1; /// return; /// } /// /// let mut bits = (!(col | pie | na)) & ((1 << n) - 1); /// while bits != 0 { /// let p = bits & -bits; /// Self::_dfs(n, result, row + 1, col | p, (pie | p) << 1, (na | p) >> 1); /// bits = bits & (bits - 1); /// } /// } /// /// ``` /// Reference /// * [LeetCode52](https://lichangke.github.io/2019/04/18/LeetCode-52-N%E7%9A%87%E5%90%8E-II(N-Queens-II)/) /// * [LeetCode52](https://www.cnblogs.com/albert1017/archive/2013/01/15/2860973.html) pub fn total_n_queens(n: i32) -> i32 { if n < 1 { return 0; } let mut result = 0; let mut cols = vec![]; let mut xy_sum = vec![]; let mut xy_sub = vec![]; let row = 0; _dfs(n, &mut result, row, &mut cols, &mut xy_sum, &mut xy_sub); result } pub fn _dfs(n: i32, result: &mut i32, row: i32, cols: &mut Vec<i32>, xy_sum: &mut Vec<i32>, xy_sub: &mut Vec<i32>) { if row >= n { *result += 1; return; } for col in 0..n { if cols.contains(&col) || xy_sum.contains(&(row + col)) || xy_sub.contains(&(row - col)) { continue; } cols.push(col); xy_sum.push(row + col); xy_sub.push(row - col); _dfs(n, result, row + 1, cols, xy_sum, xy_sub); cols.retain(|&x| x != col); xy_sum.retain(|&x| x != (row + col)); xy_sub.retain(|&x| x != (row - col)); } }