diff options
author | terminaldweller <thabogre@gmail.com> | 2022-04-12 13:47:14 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-04-12 13:47:14 +0000 |
commit | d438d97dd6844ca0caa3735f71869625df49336f (patch) | |
tree | 79060ea0dba510d43c19b0a6dc7b8b5cfc790242 /289/main.cpp | |
parent | update (diff) | |
download | leetcode-d438d97dd6844ca0caa3735f71869625df49336f.tar.gz leetcode-d438d97dd6844ca0caa3735f71869625df49336f.zip |
update
Diffstat (limited to '')
-rw-r--r-- | 289/main.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/289/main.cpp b/289/main.cpp new file mode 100644 index 0000000..f1fd989 --- /dev/null +++ b/289/main.cpp @@ -0,0 +1,81 @@ + +#include "header.hpp" + +class Solution { +public: + void gameOfLife(std::vector<std::vector<int>> &board) { + std::vector<std::vector<int>> shadow; + shadow.resize(board.size()); + /* std::cout << board.size() << "\n"; */ + /* std::cout << board[0].size() << "\n"; */ + auto M = board.size(); + auto N = board[0].size(); + for (auto &iter : shadow) { + iter.resize(N); + } + int pop = 0; + for (int i = 0; i < M; ++i) { + for (int j = 0; j < N; ++j) { + std::cout << "(i,j):" << i << "," << j << "\t"; + // Neighbour + if (i >= 1) { + pop += board[i - 1][j]; + if (j >= 1) + pop += board[i - 1][j - 1]; + if (j < N - 1) + pop += board[i - 1][j + 1]; + } + if (i < M - 1) { + pop += board[i + 1][j]; + if (j >= 1) + pop += board[i + 1][j - 1]; + if (j < N - 1) + pop += board[i + 1][j + 1]; + } + if (j < N - 1) { + pop += board[i][j + 1]; + } + if (j >= 1) { + pop += board[i][j - 1]; + } + std::cout << "pop: " << pop << "\n"; + /* std::cout << shadow[0][0] << "\n"; */ + /* std::cout << shadow[3][2] << "\n"; */ + /* std::cout << "fuck\n"; */ + + // Decision + if (board[i][j] == 0) { + if (pop == 3) + shadow[i][j] = 1; + else + shadow[i][j] = 0; + } else { + if (pop == 2 || pop == 3) + shadow[i][j] = 1; + else + shadow[i][j] = 0; + } + pop = 0; + } + } + /* std::cout << "fuck\n"; */ + + for (int i = 0; i < board.size(); ++i) { + for (int j = 0; j < board[0].size(); ++j) { + board[i][j] = shadow[i][j]; + } + } + } +}; + +int main(int argc, char **argv) { + std::vector<std::vector<int>> b1 = { + {0, 1, 0}, {0, 0, 1}, {1, 1, 1}, {0, 0, 0}}; + std::vector<std::vector<int>> b2 = {{1, 1}, {1, 0}}; + + Solution solution; + solution.gameOfLife(b1); + solution.gameOfLife(b2); + + return 0; +} |