Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // PSM4
- //
- // Created by Andrew Konstantinov on 21/03/17.
- // Copyright © 2017 Andrew Konstantinov. All rights reserved.
- //
- #include <iostream>
- #include "vector"
- #include "map"
- using namespace std;
- #define MAXN 1000
- #define MAXM 1000
- #define pb push_back
- map <pair <int , int> ,int> myMap;
- int N,M;
- int a[MAXN][MAXM];
- vector <pair<int,int> > change[2];
- vector <pair<int,int> > destroy;
- int cur = 0;
- int countNeighbours(int i,int j)
- {
- if (i < 1 || i > N || j < 1 || j > M)
- return 0;
- int sum = 0;
- int x[3] = {-1,0,1};
- int y[3] = {-1,0,1};
- for (int ii = 0; ii < 3; ++ii)
- for (int jj = 0; jj < 3; ++jj){
- if (x[ii] == 0 && y[jj] == 0)
- continue;
- sum += a[i + x[ii]] [j + y[jj]];
- }
- return sum;
- }
- int main(int argc, const char * argv[]) {
- cin >> N >> M;
- for (int i = 1; i <= N; ++i)
- for (int j = 1; j <= M; ++j){
- cin >> a[i][j];
- if (a[i][j] == 1){
- a[i][j] = 0;
- change[cur].pb( {i ,j} );
- }
- }
- int x[3] = {-1,0,1};
- int y[3] = {-1,0,1};
- bool response;
- destroy.clear();
- while (!change[cur].empty()) {
- myMap.clear();
- // cout << "==================changes===========\n";
- // for (int i = 0;i < change[cur].size(); ++i)
- // cout << change[cur][i].first << ' ' << change[cur][i].second << "\n";
- // cout << "==================changes end===========\n";
- for (int i = 0; i < change[cur].size(); ++i)
- a[change[cur][i].first][change[cur][i].second] = 1 - a[change[cur][i].first][change[cur][i].second];
- // for (int i = 0; i < destroy.size(); ++i)
- // a[destroy[i].first][destroy[i].second] = 0;
- change[1 - cur].clear();
- // destroy.clear();
- cout << "Wanna see next ? ";
- cin >> response;// 0 or 1
- if (response == false)
- break;
- for (int i = 1; i <= N; ++i){
- for (int j = 1; j <= M; ++j)
- cout << a[i][j] << ' ';
- cout << endl;
- }
- for (int i = 0; i < change[cur].size(); ++i){
- for (int ii = 0; ii < 3; ++ii)
- for (int jj = 0; jj < 3; ++jj){
- if (x[ii] == 0 && y[jj] == 0)
- continue;
- if (myMap[{change[cur][i].first + x[ii], change[cur][i].second + y[jj]}] == 1)
- continue;
- // cout << change[cur][i].first + x[ii] << " " << change[cur][i].second + y[jj] << " has " <<
- // countNeighbours(change[cur][i].first + x[ii], change[cur][i].second + y[jj]) << " neighbours\n";
- if (a[change[cur][i].first + x[ii]][change[cur][i].second + y[jj]] == 0
- && countNeighbours(change[cur][i].first + x[ii], change[cur][i].second + y[jj]) == 3)
- // if the cell is change and has 3 neighbours then it'll be ressurected;
- change[1 - cur].pb({change[cur][i].first + x[ii] , change[cur][i].second + y[jj]} );
- if (a[change[cur][i].first + x[ii]][change[cur][i].second + y[jj]] == 1
- && (countNeighbours(change[cur][i].first + x[ii], change[cur][i].second + y[jj]) > 3 ||
- countNeighbours(change[cur][i].first + x[ii], change[cur][i].second + y[jj]) < 2)
- )
- change[1 - cur].pb({change[cur][i].first + x[ii] , change[cur][i].second + y[jj]} );
- myMap[{change[cur][i].first + x[ii], change[cur][i].second + y[jj]}] = 1;
- }
- }
- cur = 1 - cur;
- }
- for (int i = 1; i <= N; ++i){
- for (int j = 1; j <= M; ++j)
- cout << a[i][j] << ' ';
- cout << endl;
- }
- cout << "GAME OVER \n" ;
- return 0;
- }
- //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement