csansoon

P10.11 P26100 Game of life (1)

Dec 26th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. void write(vector <vector <bool> >& v) {
  6.     char c;
  7.     for (int i=0; i<v.size(); ++i) {
  8.         for (int j=0; j<v[0].size(); ++j) {
  9.             cin >> c;
  10.             if (c=='B') v[i][j]=1;
  11.         }
  12.     }
  13. }
  14.  
  15. void print(vector <vector <bool> >& v) {
  16.     for (int i=0; i<v.size(); ++i) {
  17.         for (int j=0; j<v[0].size(); ++j) {
  18.             if (v[i][j]) cout<<"B";
  19.             else cout<<".";
  20.         }
  21.         cout<<endl;
  22.     }
  23. }
  24.  
  25. vector <vector <bool> > NextScreen(vector <vector <bool> >& v) {
  26.     vector <vector <bool> > vnext(v.size(),vector <bool>(v[0].size(),0));
  27.     for (int i=0; i<v.size(); ++i) {
  28.         for (int j=0; j<v[0].size(); ++j) {
  29.             int counter = 0;
  30.             if ((i-1>=0 and j-1>=0) and v[i-1][j-1]) ++counter;
  31.             if ((j-1>=0) and v[i][j-1]) ++counter;
  32.             if ((i+1<v.size() and j-1>=0) and v[i+1][j-1]) ++counter;
  33.             if ((i-1>=0) and v[i-1][j]) ++counter;
  34.             if ((i+1<v.size()) and v[i+1][j]) ++counter;
  35.             if ((i-1>=0 and j+1<v[0].size()) and v[i-1][j+1]) ++counter;
  36.             if ((j+1<v[0].size()) and v[i][j+1]) ++counter;
  37.             if ((i+1<v.size() and j+1<v[0].size()) and v[i+1][j+1]) ++counter;
  38.             if (v[i][j] and (counter==2 or counter==3)) vnext[i][j] = true;
  39.             else if ((not v[i][j]) and counter==3) vnext[i][j] = true;
  40.             else vnext[i][j] = false;
  41.         }
  42.     }
  43.     return vnext;
  44. }
  45.            
  46.    
  47.  
  48.    
  49. int main() {
  50.     int n,m;
  51.     cin>>n>>m;
  52.     while (n!=0 and m!=0) {
  53.     vector <vector <bool> > Screen(n,vector <bool>(m,0));
  54.     vector <vector <bool> > ScreenInitial(n,vector <bool>(m,0));
  55.     write(Screen);
  56.     ScreenInitial = NextScreen(Screen);
  57.     print(ScreenInitial);
  58.     cin>>n>>m;
  59.     if (n!=0 and m!=0) cout<<endl;
  60. }
  61. }
  62.  
  63. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment