aboutsummaryrefslogtreecommitdiff
path: root/n_queens.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-02-03 15:37:19 -0700
committerJack Sangdahl <[email protected]>2025-02-03 15:37:19 -0700
commit55a67f776dd9acd258cae16ffb636751666b0f40 (patch)
tree601539de43809b70ceb8228a12c35883fa300b03 /n_queens.cpp
parenta0030873dabb517594ae0394350d6715071f7fb8 (diff)
downloadleetcode-55a67f776dd9acd258cae16ffb636751666b0f40.tar.gz
leetcode-55a67f776dd9acd258cae16ffb636751666b0f40.zip
chore: Remove incomplete solutions
Diffstat (limited to 'n_queens.cpp')
-rw-r--r--n_queens.cpp43
1 files changed, 0 insertions, 43 deletions
diff --git a/n_queens.cpp b/n_queens.cpp
deleted file mode 100644
index df2457e..0000000
--- a/n_queens.cpp
+++ /dev/null
@@ -1,43 +0,0 @@
-#include "leetcode.h"
-
-class Solution {
-public:
- vector<vector<string>> solveNQueens(int n) {
- vector<vector<string>> ans;
- vector<string> nQueens(n, string(n, '.'));
- solve(ans, nQueens, 0, n);
- return ans;
- }
-
-private:
- void solve(vector<vector<string>> &ans, vector<string> &nQueens, int row,
- int &n) {
- if (row == n) {
- ans.push_back(nQueens);
- return;
- }
- for (int col = 0; col != n; ++col)
- if (isValid(nQueens, row, col, n)) {
- nQueens[row][col] = 'Q';
- solve(ans, nQueens, row + 1, n);
- nQueens[row][col] = '.';
- }
- }
- bool isValid(vector<string> &nQueens, int row, int col, int &n) {
- // check if col had queen
- for (int i = 0; i != row; ++i)
- if (nQueens[i][col] == 'Q')
- return false;
- // check if 45deg diagonal had queen
- for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; --i, --j)
- if (nQueens[i][j] == 'Q')
- return false;
- // check if 135deg diagonal had queen
- for (int i = row - 1, j = col + 1; i >= 0 && j < n; --i, ++j)
- if (nQueens[i][j] == 'Q')
- return false;
- return true;
- }
-};
-
-int main() {}