Created
February 24, 2017 04:03
-
-
Save hikilaka/f22c59d0a8c8ae4696c80b7fa607a71c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int matrixElementsSum(std::vector<std::vector<int>> matrix) { | |
int price = 0; | |
int width = matrix[0].size(); | |
int height = matrix.size(); | |
std::vector<std::pair<int, int>> haunted {}; | |
auto flat = std::accumulate(std::begin(matrix), std::end(matrix), std::vector<int>(), | |
[](auto collector, auto row) { | |
collector.insert(std::end(collector), std::begin(row), std::end(row)); | |
return collector; | |
}); | |
for (auto itr = std::begin(flat); itr != std::end(flat); ++itr) { | |
int row = std::distance(std::begin(flat), itr) % width; | |
int column = std::distance(std::begin(flat), itr) / height; | |
int cost = *itr; | |
if (cost == 0) { | |
haunted.emplace_back(row, column); | |
continue; | |
} | |
bool is_haunted = false; | |
for (int i = 0; i < column; ++i) { | |
if (std::find(std::begin(haunted), std::end(haunted), | |
std::make_pair(row, i)) != std::end(haunted)) { | |
is_haunted = true; | |
} | |
} | |
if (!is_haunted) price += cost; | |
} | |
return price; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment