From 62436de0d03896bf8ae3a92a681b086d6417bdf9 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 27 Nov 2023 23:33:58 +0100 Subject: [PATCH] =?UTF-8?q?problems(cpp):=20add=20=E2=80=9C935.=20Knight?= =?UTF-8?q?=20Dialer=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- problems/cpp/knight-dialer.cpp | 76 ++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 problems/cpp/knight-dialer.cpp diff --git a/problems/cpp/knight-dialer.cpp b/problems/cpp/knight-dialer.cpp new file mode 100644 index 0000000..b305d48 --- /dev/null +++ b/problems/cpp/knight-dialer.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include + +class Solution { + using pad_t = std::array; + + static bool isValidMove(int i) { + return i >= 0 && i < 12 && i != 9 && i != 11; + } + + static int index(int x, int y) { + if (x < 0 || x >= 3 || y < 0 || y >= 4) { + return -1; + } + + return y * 3 + x; + } + + static int add(int x, int y) { return (x + y) % 1000000007; } + + static int get(const std::vector& dp, int it, int idx) { + if (it < 0 || it >= static_cast(dp.size())) { + return 0; + } + + if (!isValidMove(idx)) { + return 0; + } + + return dp[it][idx]; + } + + static void dpIteration(std::vector& dp, int it) { + for (int i = 0; i < 12; ++i) { + if (!isValidMove(i)) { + continue; + } + + int dx = -2, dy = -1; + for (int j = 0; j < 8; ++j) { + auto next = index(i % 3 + dx, i / 3 + dy); + dp[it][i] = add(dp[it][i], get(dp, it - 1, next)); + + // adjustment of the dx, dy + dy *= -1; + + if (j % 2 == 1) { + dx *= -1; + } + + if (j % 4 == 3) { + int d = dx; + dx = dy; + dy = d; + } + } + } + } + + public: + int knightDialer(int n) { + std::vector dp(n); + + dp[0].fill(1); + dp[0][9] = 0; + dp[0][11] = 0; + + for (int i = 1; i < n; ++i) { + dpIteration(dp, i); + } + + return std::accumulate(dp.back().begin(), dp.back().end(), 0, add); + } +};