diff --git a/2202/220217-CN.cpp b/2202/220217-CN.cpp new file mode 100644 index 0000000..11f5d2e --- /dev/null +++ b/2202/220217-CN.cpp @@ -0,0 +1,39 @@ +#include +#include + +class Solution { +private: + inline static constexpr int dX[] = {-2, -1, 1, 2, 2, 1, -1, -2}; + inline static constexpr int dY[] = {1, 2, 2, 1, -1, -2, -2, -1}; + + inline static constexpr bool isValid(int n, int x, int y) { + return x >= 0 && y >= 0 && x < n && y < n; + } +public: + inline static constexpr double knightProbability(int n, int k, int row, int column) { + double dp[25][25], dpNext[25][25]; + for (int i = 0; i < n; ++i) + for (int j = 0; j < n; ++j) + dp[i][j] = 1.0; + for (int i = 0; i < k; ++i) { + for (int x = 0; x < n; ++x) { + for (int y = 0; y < n; ++y) { + dpNext[x][y] = 0.0; + for (int idx = 0; idx < 8; ++idx) { + if (isValid(n, x + dX[idx], y + dY[idx])) { + dpNext[x][y] += dp[x + dX[idx]][y + dY[idx]]; + } + } + dpNext[x][y] /= 8.0; + } + } + std::memcpy(dp, dpNext, sizeof dp); + } + return dp[row][column]; + } +}; + +int main() { + std::printf("%lf\n", Solution::knightProbability(3, 2, 0, 0)); + return 0; +} diff --git a/2202/CMakeLists.txt b/2202/CMakeLists.txt index 1da13ea..ea4aeaa 100644 --- a/2202/CMakeLists.txt +++ b/2202/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2202) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2202 220217.cpp) +ADD_EXECUTABLE(2202 220217-CN.cpp)