add: 220224-CN [cpp]
This commit is contained in:
parent
a2ae33a3a3
commit
5d08132f87
|
|
@ -0,0 +1,39 @@
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
static std::vector<int> findBall(const std::vector<std::vector<int>>& grid) {
|
||||||
|
const int n = grid.front().size();
|
||||||
|
std::vector<int> ret(n);
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
ret[i] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto it = grid.begin(); it != grid.end(); ++it) {
|
||||||
|
const std::vector<int>& 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 << ' ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,4 +3,4 @@ PROJECT(2202)
|
||||||
|
|
||||||
SET(CMAKE_CXX_STANDARD 23)
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
ADD_EXECUTABLE(2202 220223.cpp)
|
ADD_EXECUTABLE(2202 220224-CN.cpp)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue