From 5255f932d52fd0c71b2bd0282370055e75fe2607 Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Sun, 22 May 2022 23:50:16 +0800 Subject: [PATCH] add: 220522-CN [cpp] --- cpp/2205/220522-CN.cpp | 54 +++++++++++++++++++++++++++++++++++++++++ cpp/2205/CMakeLists.txt | 2 +- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 cpp/2205/220522-CN.cpp diff --git a/cpp/2205/220522-CN.cpp b/cpp/2205/220522-CN.cpp new file mode 100644 index 0000000..0515d18 --- /dev/null +++ b/cpp/2205/220522-CN.cpp @@ -0,0 +1,54 @@ +#include +#include + +/** + * 464. Can I Win + * In the "100 game" two players take turns adding, to a running total, any integer from 1 to 10. The player who first causes the running total to reach or exceed 100 wins. + * What if we change the game so that players cannot re-use integers? + * For example, two players might take turns drawing from a common pool of numbers from 1 to 15 without replacement until they reach a total >= 100. + * Given two integers maxChoosableInteger and desiredTotal, return true if the first player to move can force a win, otherwise, return false. Assume both players play optimally. + */ + +class Solution { +public: + static bool canIWin(int maxChoosableInteger, int desiredTotal) { + if (!desiredTotal) + return true; + if (maxChoosableInteger * (maxChoosableInteger + 1) / 2 < desiredTotal) + return false; + + // ans Key -> bit 19-0: chosen, bit 20: moving | 0 = me, bit -21: cur + // ans[state] -> Can I (moves first) win with this state? + std::bitset<1048576> s, vis; + + std::function dp = [&](int state) { + if (vis[state]) + return s.test(state); + + int sum = 0; + for (int i = 0; i < maxChoosableInteger; ++i) + if (state & (1 << i)) + sum += 1 + i; + + vis.set(state); + if (sum >= desiredTotal) + return bool(s[state] = false); + + for (int i = 0; i < maxChoosableInteger; ++i) { + if (state & (1 << i)) + continue; + if (!dp(state | (1 << i))) + return bool(s[state] = true); + } + return bool(s[state] = false); + }; + + return dp(0); + } +}; + +int main() { + std::printf("%s\n", Solution::canIWin(6, 16) ? "yes" : "false"); + return 0; +} + diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index 15e1b64..8699da0 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2205 220521-CN.cpp) +ADD_EXECUTABLE(2205 220522-CN.cpp)