Branch data Line data Source code
1 : : #include <iostream>
2 : : #include <queue>
3 : : #include <sstream>
4 : : #include <fstream>
5 : : #include <algorithm>
6 : : #include <array>
7 : :
8 : : using namespace std;
9 : :
10 : : constexpr array<int, 4> DIRECTIONS{0, 1, 0, -1};
11 : :
12 : 2 : int main() {
13 : 2 : ifstream inputFile("input.txt");
14 [ + + ]: 2 : if (!inputFile) {
15 : 1 : cerr << "Error: Unable to open input.txt" << endl;
16 : 1 : return 1;
17 : : }
18 : :
19 : : int xCoord;
20 : : int yCoord;
21 : 1 : int maxCount = 0;
22 : 1 : array<array<bool, 71>, 71> grid{};
23 : 1 : array<array<int, 71>, 71> dist{};
24 : 2 : queue<pair<int, int>> positions({{0, 0}});
25 : :
26 [ + - + + : 1025 : for (string line; getline(inputFile, line) && maxCount < 1024; ++maxCount) {
+ + ]
27 [ + - ]: 1024 : if (char comma; stringstream(line) >> xCoord >> comma >> yCoord) {
28 : 1024 : grid[xCoord][yCoord] = true;
29 : : }
30 : 1 : }
31 : :
32 [ + + ]: 72 : for (auto& row : dist) {
33 : 71 : row.fill(-1);
34 : : }
35 : 1 : dist[0][0] = 0;
36 [ + - ]: 3972 : while (!positions.empty()) {
37 : 3972 : auto [currentX, currentY] = positions.front();
38 : 3972 : positions.pop();
39 [ + + + + ]: 3972 : if (currentX == 70 && currentY == 70) {
40 : 1 : cout << dist[currentX][currentY] << endl;
41 : 1 : break;
42 : : }
43 : :
44 [ + + ]: 19855 : for (int i = 0; i < 4; ++i) {
45 : 15884 : int newX = currentX + DIRECTIONS[i];
46 : 15884 : int newY = currentY + DIRECTIONS[(i + 3) & 3];
47 [ + + + + : 15884 : if (newX >= 0 && newX < 71 && newY >= 0 && newY < 71 && !grid[newX][newY] && dist[newX][newY] == -1) {
+ + + + +
+ + + +
+ ]
48 : 3972 : dist[newX][newY] = dist[currentX][currentY] + 1;
49 : 3972 : positions.push({newX, newY});
50 : : }
51 : : }
52 : : }
53 [ + + ]: 3 : }
|