diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-18 06:38:01 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-18 06:38:01 +0100 |
| commit | 6bf92f2a2517f8eebddbdb139a877dcffd381edc (patch) | |
| tree | cbe299f4501d13527260daa87b852e2a996c580f /2024/18/day18a.cpp | |
| parent | a0e775b0a4805a7c1af12c1a956cd9fd74c54b2c (diff) | |
| download | aoc-6bf92f2a2517f8eebddbdb139a877dcffd381edc.tar.gz aoc-6bf92f2a2517f8eebddbdb139a877dcffd381edc.zip | |
Day 18 2024
Diffstat (limited to '2024/18/day18a.cpp')
| -rw-r--r-- | 2024/18/day18a.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/2024/18/day18a.cpp b/2024/18/day18a.cpp new file mode 100644 index 0000000..7c795a9 --- /dev/null +++ b/2024/18/day18a.cpp | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <cstdint> | ||
| 3 | #include <iostream> | ||
| 4 | #include <map> | ||
| 5 | #include <queue> | ||
| 6 | #include <ranges> | ||
| 7 | #include <set> | ||
| 8 | #include <sstream> | ||
| 9 | #include <string> | ||
| 10 | #include <string_view> | ||
| 11 | #include <vector> | ||
| 12 | using namespace std; | ||
| 13 | |||
| 14 | #define N 71 | ||
| 15 | #define S 1024 | ||
| 16 | |||
| 17 | bool b[N][N]; | ||
| 18 | |||
| 19 | class Qelem { | ||
| 20 | public: | ||
| 21 | int d, x, y; | ||
| 22 | Qelem(int dis, int xx, int yy) : d{dis}, x{xx}, y{yy} {} | ||
| 23 | bool operator<(const Qelem& other) const { return d > other.d; } | ||
| 24 | }; | ||
| 25 | |||
| 26 | |||
| 27 | int coord(int x, int y) { | ||
| 28 | return N*x+y; | ||
| 29 | } | ||
| 30 | |||
| 31 | bool inrange(int x, int y) { | ||
| 32 | return x >= 0 && x < N && y >= 0 && y < N; | ||
| 33 | } | ||
| 34 | |||
| 35 | int shortest_path(bool b[N][N]) { | ||
| 36 | vector<bool> vis(N*N); | ||
| 37 | |||
| 38 | priority_queue<Qelem> q; | ||
| 39 | q.push(Qelem(0, 0, 0)); | ||
| 40 | |||
| 41 | while (!q.empty()) { | ||
| 42 | auto v = q.top(); | ||
| 43 | q.pop(); | ||
| 44 | if (!inrange(v.x, v.y) || b[v.x][v.y] || vis[coord(v.x, v.y)]) | ||
| 45 | continue; | ||
| 46 | if (v.x == N-1 && v.y == N-1) return v.d; | ||
| 47 | vis[coord(v.x, v.y)] = true; | ||
| 48 | q.push(Qelem(v.d+1, v.x+1, v.y)); | ||
| 49 | q.push(Qelem(v.d+1, v.x-1, v.y)); | ||
| 50 | q.push(Qelem(v.d+1, v.x, v.y+1)); | ||
| 51 | q.push(Qelem(v.d+1, v.x, v.y-1)); | ||
| 52 | } | ||
| 53 | return -1; | ||
| 54 | } | ||
| 55 | |||
| 56 | int main() { | ||
| 57 | int x, y; | ||
| 58 | for (int i = 0; i < S && (cin >> x >> y); i++) | ||
| 59 | b[x][y] = true; | ||
| 60 | |||
| 61 | cout << shortest_path(b) << endl; | ||
| 62 | return 0; | ||
| 63 | } | ||
