# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1100250 | vjudge1 | Maxcomp (info1cup18_maxcomp) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}