Code : The Most Reckless Defense
#include <bits/stdc++.h>
using namespace std;
const int maxr = 13;
vector<int> pow3(maxr);
int sq(int x) { return x * x; }
bool is_set(int mask, int bit) { return ((1 << bit) & mask) != 0; }
int turn_on(int mask, int bit) { return mask | (1 << bit); }
void solve() {
int row, col, k;
cin >> row >> col >> k;
vector<vector<char>> mat(row, vector<char>(col));
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cin >> mat[i][j];
}
}
vector<vector<int>> attack(k, vector<int>(maxr, 0));
for (int idx = 0; idx < k; idx++) {
int x, y, p;
cin >> x >> y >> p;
x--, y--;
for (int r = 0; r < maxr; r++) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (mat[i][j] == '#' && (sq(x - i) + sq(y - j) <= sq(r))) {
attack[idx][r] += p;
}
}
}
}
}
int full_mask = (1 << maxr) - 1;
vector<int> dp(full_mask + 1);
for (int i = 0; i < k; i++) {
vector<int> ndp = dp;
for (int mask = 0; mask <= full_mask; mask++) {
for (int r = 0; r < maxr; r++) {
if (!is_set(mask, r)) {
int nxt = turn_on(mask, r);
ndp[nxt] = max(ndp[nxt], dp[mask] + attack[i][r]);
}
}
}
swap(ndp, dp);
}
int ans = 0;
for (int mask = 0; mask <= full_mask; mask++) {
int cost = 0;
for (int r = 0; r < maxr; r++) {
if (is_set(mask, r)) {
cost += pow3[r];
}
}
ans = max(ans, dp[mask] - cost);
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
pow3[0] = 1;
for (int r = 1; r < maxr; r++) {
pow3[r] = 3 * pow3[r - 1];
}
int t;
cin >> t;
for (int zz = 0; zz < t; zz++) {
solve();
}
return 0;
}