#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int n, m;
vector<vector<int>> mat;
struct C {
int x, y;
};
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
bool valid(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m;
}
bool connected(const vector<C>& sub) {
if (sub.empty()) return false;
vector<vector<bool>> vis(n, vector<bool>(m, false));
queue<C> q;
q.push(sub[0]);
vis[sub[0].x][sub[0].y] = true;
int cnt = 1;
while (!q.empty()) {
C curr = q.front();
q.pop();
for (int d = 0; d < 4; ++d) {
int nx = curr.x + dx[d];
int ny = curr.y + dy[d];
if (valid(nx, ny) && !vis[nx][ny]) {
for (const auto& c : sub) {
if (c.x == nx && c.y == ny) {
vis[nx][ny] = true;
q.push({nx, ny});
cnt++;
break;
}
}
}
}
}
return cnt == sub.size();
}
int weight(const vector<C>& sub) {
int maxV = INT_MIN, minV = INT_MAX;
for (const auto& c : sub) {
maxV = max(maxV, mat[c.x][c.y]);
minV = min(minV, mat[c.x][c.y]);
}
return maxV - minV - sub.size();
}
int main() {
cin >> n >> m;
mat.resize(n, vector<int>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> mat[i][j];
}
}
int maxW = INT_MIN;
int totalCells = n * m;
for (int mask = 1; mask < (1 << totalCells); ++mask) {
vector<C> sub;
for (int i = 0; i < totalCells; ++i) {
if (mask & (1 << i)) {
int x = i / m;
int y = i % m;
sub.push_back({x, y});
}
}
if (connected(sub)) {
int w = weight(sub);
maxW = max(maxW, w);
}
}
cout << maxW << endl;
return 0;
}
Compilation message
maxcomp.cpp: In function 'bool connected(const std::vector<C>&)':
maxcomp.cpp:53:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<C>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | return cnt == sub.size();
| ~~~~^~~~~~~~~~~~~
maxcomp.cpp: In function 'int weight(const std::vector<C>&)':
maxcomp.cpp:57:16: error: 'INT_MIN' was not declared in this scope
57 | int maxV = INT_MIN, minV = INT_MAX;
| ^~~~~~~
maxcomp.cpp:5:1: note: 'INT_MIN' is defined in header '<climits>'; did you forget to '#include <climits>'?
4 | #include <algorithm>
+++ |+#include <climits>
5 |
maxcomp.cpp:60:9: error: 'minV' was not declared in this scope
60 | minV = min(minV, mat[c.x][c.y]);
| ^~~~
maxcomp.cpp:63:19: error: 'minV' was not declared in this scope
63 | return maxV - minV - sub.size();
| ^~~~
maxcomp.cpp: In function 'int main()':
maxcomp.cpp:76:16: error: 'INT_MIN' was not declared in this scope
76 | int maxW = INT_MIN;
| ^~~~~~~
maxcomp.cpp:76:16: note: 'INT_MIN' is defined in header '<climits>'; did you forget to '#include <climits>'?