#include <iomanip>
#include <iostream>
#include <random>
#include <vector>
using namespace std;
using value_t = int;
using row_t = vector<value_t>;
using matrix_t = vector<row_t>;
class TargetElement {
public:
explicit TargetElement(const matrix_t& matrix) : matrix(matrix) {}
TargetElement& target(const size_t i, const size_t j) {
r = i;
c = j;
return *this;
}
size_t rows() const { return matrix.size(); }
size_t columns() const { return matrix[0].size(); }
void show(const streamsize w) const {
cout.put('\n');
for (const auto& row : matrix) {
for (auto value : row) cout << setw(w) << value;
cout.put('\n');
}
cout << '\n' << *this << '\n';
}
private:
size_t r = 0;
size_t c = 0;
matrix_t matrix;
value_t sum() const {
const auto top = r != 0 ? matrix[r - 1][c] : 0;
const auto left = c != 0 ? matrix[r][c - 1] : 0;
const auto bottom = r != matrix.size() - 1 ? matrix[r + 1][c] : 0;
const auto right = c != matrix[0].size() - 1 ? matrix[r][c + 1] : 0;
return top + left + bottom + right;
}
friend bool operator<(const TargetElement& a, const TargetElement& b) {
return a.sum() < b.sum();
}
friend ostream& operator<<(ostream& out, const TargetElement& pos) {
out << pos.matrix[pos.r][pos.c]
<< ' ' << pos.r + 1 << ' ' << pos.c + 1
<< ' ' << pos.sum();
return out;
}
};
TargetElement task(const matrix_t& matrix) {
TargetElement te(matrix);
const auto rows = te.rows();
const auto columns = te.columns();
TargetElement max = te.target(0, 0);
for (size_t i = 0; i < rows; ++i)
for (size_t j = 0; j < columns; ++j)
if (max < te.target(i, j))
max = te;
return max;
}
size_t set_size(const char* prompt, const size_t limit) {
size_t value = 0;
while (!value || limit < value) {
cout << prompt;
cin >> value;
cin.ignore(0x1000, '\n');
}
return value;
}
void fill_random(matrix_t& matrix, value_t l, value_t r) {
if (r < l) swap(l, r);
uniform_int_distribution<> uid(l, r);
mt19937 gen{ random_device()() };
for (auto& row : matrix)
for (auto& value : row)
value = uid(gen);
}
int main() {
const auto m = set_size("m: ", 10);
const auto n = set_size("n: ", 12);
matrix_t matrix(m, row_t(n));
fill_random(matrix, -9, 9);
task(matrix).show(5);
}