diff --git a/cpp/2202/220224-CN.cpp b/cpp/2202/220224-CN.cpp new file mode 100644 index 0000000..32dd320 --- /dev/null +++ b/cpp/2202/220224-CN.cpp @@ -0,0 +1,39 @@ +#include +#include + +class Solution { +public: + static std::vector findBall(const std::vector>& grid) { + const int n = grid.front().size(); + std::vector ret(n); + for (int i = 0; i < n; ++i) { + ret[i] = i; + } + + for (auto it = grid.begin(); it != grid.end(); ++it) { + const std::vector& now = *it; + for (int i = 0; i < n; ++i) { + if (ret[i] == -1) + continue; + if (ret[i] < n - 1 && now[ret[i]] == 1 && now[ret[i] + 1] == 1) { // goes R + ++ret[i]; + } else if (ret[i] && now[ret[i]] == -1 && now[ret[i] - 1] == -1) { // goes L + --ret[i]; + } else { // nowhere to go! + ret[i] = -1; + } + } + } + + return ret; + } +}; + +int main() { + // auto ret = Solution::findBall({{1,1,1,-1,-1},{1,1,1,-1,-1},{-1,-1,-1,1,1},{1,1,1,1,-1},{-1,-1,-1,-1,-1}}); + // auto ret = Solution::findBall({{-1}}); + auto ret = Solution::findBall({{1,1,1,1,1,1},{-1,-1,-1,-1,-1,-1},{1,1,1,1,1,1},{-1,-1,-1,-1,-1,-1}}); + for (auto i : ret) { + std::cout << i << ' '; + } +} diff --git a/cpp/2202/CMakeLists.txt b/cpp/2202/CMakeLists.txt index d97d44b..eeede82 100644 --- a/cpp/2202/CMakeLists.txt +++ b/cpp/2202/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2202) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2202 220223.cpp) +ADD_EXECUTABLE(2202 220224-CN.cpp)