-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_10.cpp
83 lines (68 loc) · 1.84 KB
/
day_10.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
80
81
82
83
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
typedef vector<string> vs;
void dfs(vector<string> &in, int i, int j, int &score, vector<vector<bool>>& visited, bool unique) {
if (!visited[i][j] && in[i][j] == '9') {
score++;
if (unique)
visited[i][j] = true;
return;
}
if (i + 1 < in.size() && (in[i + 1][j] - in[i][j]) == 1) {
dfs(in, i + 1, j, score, visited, unique);
}
if (i - 1 >= 0 && (in[i - 1][j] - in[i][j]) == 1) {
dfs(in, i - 1, j, score, visited, unique);
}
if (j + 1 < in[i].size() && (in[i][j + 1] - in[i][j]) == 1) {
dfs(in, i, j + 1, score, visited, unique);
}
if (j - 1 >= 0 && (in[i][j - 1] - in[i][j]) == 1) {
dfs(in, i, j - 1, score, visited, unique);
}
}
void solve_1(vs input) {
int total_score = 0;
for (int i = 0; i < input.size(); i++) {
int score = 0;
for (int j = 0; j < input[i].size(); j++) {
vector<vector<bool>> visited(input.size(), vector<bool>(input[0].size(), false));
if (input[i][j] == '0') {
dfs(input, i, j, score, visited, true);
}
}
total_score += score;
}
cout << "first solution: " << total_score << endl;
}
void solve_2(vs input) {
int total_score = 0;
for (int i = 0; i < input.size(); i++) {
int score = 0;
for (int j = 0; j < input[i].size(); j++) {
vector<vector<bool>> visited(input.size(), vector<bool>(input[0].size(), false));
if (input[i][j] == '0') {
dfs(input, i, j, score, visited, false);
}
}
total_score += score;
}
cout << "second solution: " << total_score << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
vector<string> input;
string s;
while (cin >> s) {
input.push_back(s);
}
solve_1(input);
solve_2(input);
return 0;
}