Branch data Line data Source code
1 : : #include <iostream>
2 : : #include <fstream>
3 : : #include <vector>
4 : : #include <array>
5 : : #include <queue>
6 : : #include <sstream>
7 : :
8 : : using namespace std;
9 : :
10 : : static constexpr array<array<int, 2>, 4> kNeighborDeltas{{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}};
11 : :
12 : 2 : int main() {
13 : 2 : ifstream input("input.txt");
14 [ + + ]: 2 : if (!input) {
15 : 1 : cerr << "Error: input.txt not found or inaccessible." << endl;
16 : 1 : return 1;
17 : : }
18 : 1 : queue<pair<int, int>> q;
19 : 1 : vector<vector<char>> grid;
20 : 1 : vector<pair<int,int>> zeroPositions;
21 [ + + + - : 57 : for (string line; getline(input, line) && !line.empty();grid.emplace_back(line.begin(), line.end())) {
+ + ]
22 [ + + ]: 3192 : for (int i = 0; i < line.size(); ++i) {
23 [ + + ]: 3136 : if(line[i]=='0') {
24 : 296 : zeroPositions.emplace_back(i, grid.size());
25 : : }
26 : : }
27 : 1 : }
28 : 1 : int totalScore = 0;
29 : 1 : auto rows = static_cast<int>(grid.size());
30 : 1 : auto cols = static_cast<int>(grid[0].size());
31 : 1 : vector<vector<int>> visited(rows, vector<int>(cols, -1));
32 : 1 : int stamp = 0;
33 [ + + ]: 297 : for (auto [x, y] : zeroPositions) {
34 : 296 : ++stamp;
35 : 296 : q.push({ x, y });
36 : 296 : visited[y][x] = stamp;
37 [ + + ]: 5491 : while (!q.empty()) {
38 : 5195 : auto [currentX, currentY] = q.front();
39 : 5195 : q.pop();
40 [ + + ]: 5195 : if (grid[currentY][currentX] == '9') {
41 : 694 : ++totalScore;
42 : 694 : continue;
43 : : }
44 [ + + ]: 22505 : for (auto [dx, dy] : kNeighborDeltas) {
45 : 18004 : int neighborX = currentX + dx;
46 : 18004 : int neighborY = currentY + dy;
47 [ + + + + : 18004 : if (neighborX >= 0 && neighborX < cols && neighborY >= 0 && neighborY < rows && visited[neighborY][neighborX] != stamp && grid[neighborY][neighborX] == grid[currentY][currentX] + 1) {
+ + + + +
+ + + +
+ ]
48 : 4899 : visited[neighborY][neighborX] = stamp;
49 : 4899 : q.push({ neighborX, neighborY });
50 : : }
51 : : }
52 : : }
53 : : }
54 : 1 : cout << totalScore << endl;
55 [ + + ]: 3 : }
|