-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_24b.cpp
141 lines (131 loc) · 3.91 KB
/
day_24b.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <limits>
#include <queue>
#include <string>
#include <unordered_set>
#include <vector>
struct Point {
Point(const int row = 0, const int col = 0) : row(row), col(col) {}
int row;
int col;
bool operator == (const Point& p) const {
return row == p.row && col == p.col;
}
};
struct Cost {
int already_incurred = 0;
int heuristic = 0;
int total = 0;
};
struct PointWithCost {
Point point;
Cost cost;
bool operator == (const PointWithCost& pc) const {
return point == pc.point;
}
};
struct Comparator {
bool operator () (const PointWithCost& p1, const PointWithCost& p2) {
return p1.cost.total > p2.cost.total;
}
};
struct hasher {
std::size_t operator () (const PointWithCost& pc) const {
return pc.point.row << 10 + pc.point.col;
}
};
int calculate_distance(const Point& p1, const Point& p2, const std::vector<std::string>& map) {
std::unordered_set<PointWithCost, hasher> seen;
const std::vector<Point> moves {
Point(0,1),
Point(1,0),
Point(0,-1),
Point(-1,0)
};
PointWithCost start;
start.point = p1;
std::priority_queue<PointWithCost, std::vector<PointWithCost>, Comparator> pq;
pq.push(start);
while(!pq.empty()) {
const auto current = pq.top();
pq.pop();
if (seen.find(current) != seen.end()) continue;
seen.insert(current);
if (current.point.row == p2.row && current.point.col == p2.col) {
return current.cost.already_incurred;
}
for (const auto& move : moves) {
PointWithCost next;
next.point.row = current.point.row + move.row;
next.point.col = current.point.col + move.col;
if (next.point.row >= map.size() || next.point.row < 0 || next.point.col >= map[0].size() || next.point.col < 0 || map[next.point.row][next.point.col] == '#') {
continue;
}
next.cost.already_incurred = current.cost.already_incurred + 1;
next.cost.heuristic = std::abs(p2.row - next.point.row) + std::abs(p2.col - next.point.col);
next.cost.total = next.cost.heuristic + next.cost.already_incurred;
pq.push(next);
}
}
std::cout << "path not found" << '\n';
return -1;
}
void dfs_util(int current, const std::vector<std::vector<int>>& am, std::vector<bool>& seen, int cost, int& min_cost) {
if (all_of(std::begin(seen), std::end(seen), [](const auto ele) {return ele;})) {
min_cost = std::min(cost, min_cost);
return;
}
for (int i = 0; i < seen.size(); i++) {
if (seen[i]) continue;
seen[i] = true;
cost += am[current][i];
dfs_util(i,am,seen,cost, min_cost);
cost -= am[current][i];
seen[i] = false;
}
}
int dfs(const std::vector<std::vector<int>>& am) {
std::vector<bool> seen(am.size(), false);
seen[0] = true;
int min_cost = std::numeric_limits<int>::max();
dfs_util(0, am, seen, 0, min_cost);
return min_cost;
}
int main(int argc, char* argv[]) {
std::string input = "../input/day_24_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
std::vector<std::string> map;
std::vector<Point> points;
while(std::getline(file, line)) {
map.push_back(line);
for (int idx = 0; idx < line.size(); idx++) {
const char c = line[idx];
if (c >= '0' && c <= '9') {
Point p;
p.row = map.size() - 1;
p.col = idx;
if (points.size() <= (c - '0')) {
points.resize(c - '0' + 1);
}
points[c-'0'] = p;
}
}
}
std::vector<std::vector<int>> adjacency_matrix(points.size(), std::vector<int>(points.size()));
for (int idx_1 = 0; idx_1 < points.size(); idx_1++) {
for (int idx_2 = idx_1+1; idx_2 < points.size(); idx_2++) {
const auto dist = calculate_distance(points[idx_1], points[idx_2], map);
adjacency_matrix[idx_1][idx_2] = dist;
adjacency_matrix[idx_2][idx_1] = dist;
}
}
std::cout << dfs (adjacency_matrix) << '\n';
return 0;
}