#include using namespace std; int main(){ ifstream input_file("input.txt"); string line; vector> tree_height_map; while(getline(input_file, line)){ vector acc; for(char c : line){ acc.push_back(c - '0'); } tree_height_map.push_back(acc); } size_t WIDTH = tree_height_map.at(0).size(); size_t HEIGHT = tree_height_map.size(); vector> spotted_trees; vector dummy(WIDTH, false); for(size_t i = 0; i < HEIGHT; ++i){ spotted_trees.push_back(dummy); } int result = 0; // Now check from all directions // Left to right first for(size_t i = 0; i < HEIGHT; ++i){ int highest_tree = -1; for(size_t j = 0; j < WIDTH; ++j){ if(tree_height_map[i][j] > highest_tree){ highest_tree = tree_height_map[i][j]; if(!spotted_trees[i][j]){ result++; spotted_trees[i][j] = true; } } } } // From right to left for(size_t i = 0; i < HEIGHT; ++i){ int highest_tree = -1; for(size_t j = WIDTH; j > 0; --j){ if(tree_height_map[i][j-1] > highest_tree){ highest_tree = tree_height_map[i][j-1]; if(!spotted_trees[i][j-1]){ result++; spotted_trees[i][j-1] = true; } } } } // From top to bottom for(size_t j = 0; j < WIDTH; ++j){ int highest_tree = -1; for(size_t i = 0; i < HEIGHT; ++i){ if(tree_height_map[i][j] > highest_tree){ highest_tree = tree_height_map[i][j]; if(!spotted_trees[i][j]){ result++; spotted_trees[i][j] = true; } } } } // From bottom to top for(size_t j = 0; j < WIDTH; ++j){ int highest_tree = -1; for(size_t i = HEIGHT; i > 0; --i){ if(tree_height_map[i-1][j] > highest_tree){ highest_tree = tree_height_map[i-1][j]; if(!spotted_trees[i-1][j]){ result++; spotted_trees[i-1][j] = true; } } } } cout << "Number of spotted trees: " << result << endl; return 0; }