From cc0d59dca67bf6dd0043f76bfb9d271b96b963e7 Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Mon, 6 Jun 2022 00:39:06 +0800 Subject: [PATCH] add: 220605 + CN --- cpp/2206/220605-CN.cpp | 19 +++++++++++++++++++ cpp/2206/220605.cpp | 38 ++++++++++++++++++++++++++++++++++++++ cpp/2206/CMakeLists.txt | 2 +- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 cpp/2206/220605-CN.cpp create mode 100644 cpp/2206/220605.cpp diff --git a/cpp/2206/220605-CN.cpp b/cpp/2206/220605-CN.cpp new file mode 100644 index 0000000..86a5ab4 --- /dev/null +++ b/cpp/2206/220605-CN.cpp @@ -0,0 +1,19 @@ +#include +#include +#include + +class Solution { +private: + double radius, x_center, y_center; + std::mt19937_64 r; + +public: + Solution(double radius, double xCenter, double yCenter) : radius(radius), x_center(xCenter), y_center(yCenter) { + r = std::mt19937_64(std::time(nullptr)); + } + + std::vector randPoint() { + double l = double (r()) / std::mt19937_64::max() * radius, d = double (r()) / std::mt19937_64::max() * std::acos(-1.0); + return {x_center + std::sin(d) * l, y_center + std::cos(d) * l}; + } +}; diff --git a/cpp/2206/220605.cpp b/cpp/2206/220605.cpp new file mode 100644 index 0000000..580e2c4 --- /dev/null +++ b/cpp/2206/220605.cpp @@ -0,0 +1,38 @@ +#include + +/** + * 52. N-Queens II + * The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. + * Given an integer n, return the number of distinct solutions to the n-queens puzzle. + */ + +class Solution { +public: + static int totalNQueens(int n) { + int ret = 0; + + bool xs[10]{}, ds[20]{}, sds[20]{}, G[10][10]{}; + std::function dfs = [&](int d) { + if (d >= n) { + ++ret; + return; + } + for (int i = 0; i < n; ++i) { + if (xs[i] || ds[d - i + 10] || sds[d + i]) + continue; + G[d][i] = xs[i] = ds[d - i + 10] = sds[d + i] = true; + dfs(1 + d); + G[d][i] = xs[i] = ds[d - i + 10] = sds[d + i] = false; + } + }; + + dfs(0); + + return ret; + } +}; + +int main() { + auto ret = Solution::totalNQueens(4); + return 0; +} diff --git a/cpp/2206/CMakeLists.txt b/cpp/2206/CMakeLists.txt index 0d5f52f..4ae8a65 100644 --- a/cpp/2206/CMakeLists.txt +++ b/cpp/2206/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2206) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2206 220604.cpp) +ADD_EXECUTABLE(2206 220605-CN.cpp)