-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday23.cpp
More file actions
77 lines (62 loc) · 1.81 KB
/
day23.cpp
File metadata and controls
77 lines (62 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <vector>
using namespace std;
// Function to count the shimmering neighbors of a cell
int countNeighbors(const vector<vector<int>>& grid, int x, int y, int m, int n) {
int count = 0;
// Directions for the 8 neighbors
int directions[8][2] = {
{-1, -1}, {-1, 0}, {-1, 1},
{ 0, -1}, { 0, 1},
{ 1, -1}, { 1, 0}, { 1, 1}
};
for (auto& dir : directions) {
int nx = x + dir[0];
int ny = y + dir[1];
if (nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny] == 1) {
count++;
}
}
return count;
}
// Function to compute the next state of the grid
vector<vector<int>> nextState(const vector<vector<int>>& grid, int m, int n) {
vector<vector<int>> newGrid(m, vector<int>(n, 0));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int neighbors = countNeighbors(grid, i, j, m, n);
if (grid[i][j] == 1) {
if (neighbors == 2 || neighbors == 3) {
newGrid[i][j] = 1; // Continues to shimmer
}
} else {
if (neighbors == 3) {
newGrid[i][j] = 1; // Becomes shimmering
}
}
}
}
return newGrid;
}
int main() {
int T;
cin >> T;
while (T--) {
int m, n;
cin >> m >> n;
vector<vector<int>> grid(m, vector<int>(n));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
cin >> grid[i][j];
}
}
vector<vector<int>> result = nextState(grid, m, n);
for (const auto& row : result) {
for (int cell : row) {
cout << cell << " ";
}
cout << endl;
}
}
return 0;
}