Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- void gen(int n, int m, vector<vector<int>>&matrix){
- for (int i = 0; i < n; i++){
- for (int j = 0; j < m; j++){
- matrix[i][j] = rand() % 2;
- }
- }
- }
- void read(int n, int m, vector<vector<int>>&matrix){
- for (int i = 0; i < n; i++){
- for (int j = 0; j < m; j++){
- cin >> matrix[i][j];
- }
- }
- }
- void write(int n, int m, vector<vector<int>>&matrix){
- for (int i = 0; i < n; i++){
- for (int j = 0; j < m; j++){
- cout << matrix[i][j] << ' ';
- }
- cout << '\n';
- }
- }
- bool check(int n, int m, int newi, int newj){
- return 0 <= newi && newi < n && 0 <= newj && newj < m;
- }
- void change_matrix(int n, int m, vector<vector<int>>&matrix){
- vector <pair<int, int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {1, 1}, {-1, 1}, {1, -1}};
- vector <pair<int, int>> change_ids;
- int cnt = 0;
- for (int i = 0; i < n; i++){
- for (int j = 0; j < m; j++){
- cnt = 0;
- for (auto dir: directions){
- int neigbori = dir.first + i;
- int neigborj = dir.second + j;
- if (check(n, m, neigbori, neigborj) && matrix[neigbori][neigborj] == 1){
- cnt += 1;
- }
- }
- if (matrix[i][j] == 1 && (cnt < 2 ||cnt > 3)){
- change_ids.push_back({i, j});
- } else if (matrix[i][j] == 0 && cnt == 3){
- change_ids.push_back({i, j});
- }
- }
- }
- if (change_ids.size() == 0){
- cout << "That's all" << '\n';
- exit(0);
- }
- for (auto id: change_ids) {
- matrix[id.first][id.second] ^= 1;
- }
- }
- int main() {
- int n, m;
- int type;
- cout << "Write n, m = " << '\n';
- cin >> n >> m;
- vector <vector<int>> matrix(n, vector<int>(m));
- cout << "Write 1, if you want to generate matrix, write 2, if you want to enter the matrix" << '\n';
- cin >> type;
- if (type == 1){
- gen(n, m, matrix);
- cout << "Generated matrix" << '\n';
- write(n, m, matrix);
- cout << '\n';
- } else {
- freopen("input.txt", "r", stdin); // read from file
- read(n, m, matrix);
- }
- while (true) {
- change_matrix(n, m, matrix);
- cout << "Result" << '\n';
- write(n, m, matrix);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement