Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5. void gen(int n, int m, vector<vector<int>>&matrix){
  6.     for (int i = 0; i < n; i++){
  7.         for (int j = 0; j < m; j++){
  8.             matrix[i][j] = rand() % 2;
  9.         }
  10.     }
  11. }
  12. void read(int n, int m, vector<vector<int>>&matrix){
  13.     for (int i = 0; i < n; i++){
  14.         for (int j = 0; j < m; j++){
  15.             cin >> matrix[i][j];
  16.         }
  17.     }
  18. }
  19. void write(int n, int m, vector<vector<int>>&matrix){
  20.     for (int i = 0; i < n; i++){
  21.         for (int j = 0; j < m; j++){
  22.             cout << matrix[i][j] << ' ';
  23.         }
  24.         cout << '\n';
  25.     }
  26. }
  27. bool check(int n, int m, int newi, int newj){
  28.     return 0 <= newi && newi < n && 0 <= newj && newj < m;
  29. }
  30. void change_matrix(int n, int m, vector<vector<int>>&matrix){
  31.     vector <pair<int, int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {1, 1}, {-1, 1}, {1, -1}};
  32.     vector <pair<int, int>> change_ids;
  33.     int cnt = 0;
  34.     for (int i = 0; i < n; i++){
  35.         for (int j = 0; j < m; j++){
  36.             cnt = 0;
  37.             for (auto dir: directions){
  38.                 int neigbori = dir.first + i;
  39.                 int neigborj = dir.second + j;
  40.                 if (check(n, m, neigbori, neigborj) && matrix[neigbori][neigborj] == 1){
  41.                     cnt += 1;
  42.                 }
  43.             }
  44.             if (matrix[i][j] == 1 && (cnt < 2 ||cnt > 3)){
  45.                 change_ids.push_back({i, j});
  46.             } else if (matrix[i][j] == 0 && cnt == 3){
  47.                 change_ids.push_back({i, j});
  48.             }
  49.         }
  50.     }
  51.     if (change_ids.size() == 0){
  52.         cout << "That's all" << '\n';
  53.         exit(0);
  54.     }
  55.     for (auto id: change_ids) {
  56.         matrix[id.first][id.second] ^= 1;
  57.     }
  58. }
  59. int main() {
  60.     int n, m;
  61.     int type;
  62.     cout << "Write n, m = " << '\n';
  63.     cin >> n >> m;
  64.     vector <vector<int>> matrix(n, vector<int>(m));
  65.     cout << "Write 1, if you want to generate matrix, write 2, if you want to enter the matrix" << '\n';
  66.     cin >> type;
  67.     if (type == 1){
  68.         gen(n, m, matrix);
  69.         cout << "Generated matrix" << '\n';
  70.         write(n, m, matrix);
  71.         cout << '\n';
  72.     } else {
  73.         freopen("input.txt", "r", stdin); // read from file
  74.         read(n, m, matrix);
  75.     }
  76.     while (true) {
  77.         change_matrix(n, m, matrix);
  78.         cout << "Result" << '\n';
  79.         write(n, m, matrix);
  80.     }
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement