Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // PSM4
  4. //
  5. // Created by Andrew Konstantinov on 21/03/17.
  6. // Copyright © 2017 Andrew Konstantinov. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include "vector"
  11. #include "map"
  12. using namespace std;
  13.  
  14. #define MAXN 1000
  15. #define MAXM 1000
  16. #define pb push_back
  17. map <pair <int , int> ,int> myMap;
  18. int N,M;
  19. int a[MAXN][MAXM];
  20.  
  21. vector <pair<int,int> > change[2];
  22. vector <pair<int,int> > destroy;
  23. int cur = 0;
  24. int countNeighbours(int i,int j)
  25. {
  26. if (i < 1 || i > N || j < 1 || j > M)
  27. return 0;
  28. int sum = 0;
  29. int x[3] = {-1,0,1};
  30. int y[3] = {-1,0,1};
  31. for (int ii = 0; ii < 3; ++ii)
  32. for (int jj = 0; jj < 3; ++jj){
  33. if (x[ii] == 0 && y[jj] == 0)
  34. continue;
  35. sum += a[i + x[ii]] [j + y[jj]];
  36. }
  37. return sum;
  38.  
  39. }
  40.  
  41. int main(int argc, const char * argv[]) {
  42.  
  43.  
  44. cin >> N >> M;
  45. for (int i = 1; i <= N; ++i)
  46. for (int j = 1; j <= M; ++j){
  47. cin >> a[i][j];
  48. if (a[i][j] == 1){
  49. a[i][j] = 0;
  50. change[cur].pb( {i ,j} );
  51. }
  52. }
  53.  
  54. int x[3] = {-1,0,1};
  55. int y[3] = {-1,0,1};
  56. bool response;
  57. destroy.clear();
  58. while (!change[cur].empty()) {
  59. myMap.clear();
  60. // cout << "==================changes===========\n";
  61. // for (int i = 0;i < change[cur].size(); ++i)
  62. // cout << change[cur][i].first << ' ' << change[cur][i].second << "\n";
  63. // cout << "==================changes end===========\n";
  64. for (int i = 0; i < change[cur].size(); ++i)
  65. a[change[cur][i].first][change[cur][i].second] = 1 - a[change[cur][i].first][change[cur][i].second];
  66. // for (int i = 0; i < destroy.size(); ++i)
  67. // a[destroy[i].first][destroy[i].second] = 0;
  68. change[1 - cur].clear();
  69. // destroy.clear();
  70. cout << "Wanna see next ? ";
  71. cin >> response;// 0 or 1
  72. if (response == false)
  73. break;
  74. for (int i = 1; i <= N; ++i){
  75. for (int j = 1; j <= M; ++j)
  76. cout << a[i][j] << ' ';
  77. cout << endl;
  78. }
  79. for (int i = 0; i < change[cur].size(); ++i){
  80. for (int ii = 0; ii < 3; ++ii)
  81. for (int jj = 0; jj < 3; ++jj){
  82. if (x[ii] == 0 && y[jj] == 0)
  83. continue;
  84. if (myMap[{change[cur][i].first + x[ii], change[cur][i].second + y[jj]}] == 1)
  85. continue;
  86. // cout << change[cur][i].first + x[ii] << " " << change[cur][i].second + y[jj] << " has " <<
  87. // countNeighbours(change[cur][i].first + x[ii], change[cur][i].second + y[jj]) << " neighbours\n";
  88.  
  89. if (a[change[cur][i].first + x[ii]][change[cur][i].second + y[jj]] == 0
  90. && countNeighbours(change[cur][i].first + x[ii], change[cur][i].second + y[jj]) == 3)
  91. // if the cell is change and has 3 neighbours then it'll be ressurected;
  92. change[1 - cur].pb({change[cur][i].first + x[ii] , change[cur][i].second + y[jj]} );
  93. if (a[change[cur][i].first + x[ii]][change[cur][i].second + y[jj]] == 1
  94. && (countNeighbours(change[cur][i].first + x[ii], change[cur][i].second + y[jj]) > 3 ||
  95. countNeighbours(change[cur][i].first + x[ii], change[cur][i].second + y[jj]) < 2)
  96. )
  97. change[1 - cur].pb({change[cur][i].first + x[ii] , change[cur][i].second + y[jj]} );
  98. myMap[{change[cur][i].first + x[ii], change[cur][i].second + y[jj]}] = 1;
  99.  
  100.  
  101.  
  102. }
  103. }
  104. cur = 1 - cur;
  105.  
  106. }
  107. for (int i = 1; i <= N; ++i){
  108. for (int j = 1; j <= M; ++j)
  109. cout << a[i][j] << ' ';
  110. cout << endl;
  111. }
  112.  
  113. cout << "GAME OVER \n" ;
  114. return 0;
  115. }
  116. //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