-
Notifications
You must be signed in to change notification settings - Fork 0
/
18.2.cpp
79 lines (75 loc) · 1.82 KB
/
18.2.cpp
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
78
79
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#include <limits>
#include <cctype>
#include <unordered_map>
#include <functional>
int getOnNeighbors(const std::vector<std::vector<char>> & lights, int row, int col);
int main()
{
std::vector<std::vector<char>> lights(100);
std::vector<std::vector<char>> new_lights;
char ch;
std::ifstream fin("test.txt");
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
fin >> ch;
lights[i].push_back(ch);
}
}
new_lights = lights;
for (int times = 0; times < 100; times++)
{
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
int count = getOnNeighbors(lights, i, j);
if (lights[i][j] == '#')
{
if (count == 2 || count == 3)
new_lights[i][j] = '#';
else
new_lights[i][j] = '.';
}
else
{
if (count == 3)
new_lights[i][j] = '#';
else
new_lights[i][j] = '.';
}
}
}
lights = new_lights;
lights[0][0] = lights[0][99] = lights[99][0] = lights[99][99] = '#';
}
int counter = 0;
for (int i = 0; i < 100; i++)
for (int j = 0; j < 100; j++)
if (lights[i][j] == '#')
counter++;
std::cout << counter;
}
int getOnNeighbors(const std::vector<std::vector<char>> & lights, int row, int col)
{
int counter = 0;
int start_row = (row == 0 ? row : row - 1);
int end_row = (row == lights.size() - 1 ? row : row + 1);
int start_col = (col == 0 ? col : col - 1);
int end_col = (col == lights[row].size() - 1 ? col : col + 1);
for (int i = start_row; i <= end_row; i++)
for (int j = start_col; j <= end_col; j++)
if (lights[i][j] == '#')
if (!(i == row && j == col))
counter++;
return counter;
}