Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <set>
- template<typename Type, size_t Rank>
- bool magical(Type(&matrix)[Rank][Rank]) {
- auto last = Rank * Rank;
- std::set<Type> tmp;
- auto s = 0u;
- for (const auto &row : matrix) {
- for (const auto value : row) {
- if (value <= 0 || value > Type(last)) return false;
- s += value;
- tmp.insert(value);
- }
- }
- if (s != last * (last + 1) / 2 || tmp.size() < last) return false;
- auto sum = s / Rank;
- auto a = 0u;
- auto b = 0u;
- for (auto i = 0u; i < Rank; ++i) {
- a += matrix[i][i];
- b += matrix[i][Rank - i - 1];
- }
- if (a != sum || b != sum) return false;
- for (auto r = 0u; r < Rank; ++r) {
- a = b = 0u;
- for (auto c = 0u; c < Rank; ++c) {
- a += matrix[r][c];
- b += matrix[c][r];
- }
- if (a != sum || b != sum) return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement