/**
* author: phuonglinhn09
* created: 11.02.2025
*/
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
int n, m, k;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
int a[n + 5][m + 5];
int tplt[n + 5][m + 5];
memset (a, 0, sizeof(a));
memset (tplt, 0, sizeof(tplt));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
cin >> k;
int x, y;
while (k--) {
cin >> x >> y;
x++, y++;
tplt[x][y] = 2;
int row, col;
for (int i = 0; i < 4; i++) {
row = x + dx[i], col = y + dy[i];
if (row >= 1 && row <= n && col >= 1 && col <= m) {
tplt[row][col] = 1;
}
}
}
bool vis[n + 5][m + 5];
memset (vis, 0, sizeof(vis));
queue<pair<int, int>> q;
int sum, mi, cnt1, cnt2;
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (tplt[i][j] != 0 && !vis[i][j]) {
sum = cnt1 = cnt2 = 0;
mi = 1e9 + 1;
q.emplace(i, j);
while (!q.empty()) {
auto [row, col] = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
x = row + dx[i], y = col + dy[i];
if (x >= 1 && x <= n && y >= 1 && y <= m && tplt[x][y] != 0 && !vis[x][y]) {
mi = min (mi, a[x][y]);
sum += a[x][y];
cnt1 += tplt[x][y] == 1;
cnt2 += tplt[x][y] == 2;
vis[x][y] = 1;
q.emplace(x, y);
}
}
}
if (cnt1 < cnt2 * 3) {
cout << "No";
return 0;
} else if (cnt1 == cnt2 * 3) {
ans += sum;
} else {
ans += sum - mi;
}
}
}
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |