题源:https://nanti.jisuanke.com/t/A1538
#include <bits/stdc++.h>
#define numm ch - 48
#define pd putchar(' ')
#define pn putchar('\n')
#define pb push_back
#define fi first
#define se second
#define fre1 freopen("1.txt", "r", stdin)
#define fre2 freopen("2.txt", "w", stdout)
typedef long long int ll;
typedef long long int LL;
using namespace std;
template<typename T>
void read(T &res) {
bool flag = false;
char ch;
while (!isdigit(ch = getchar())) (ch == '-') && (flag = true);
for (res = numm; isdigit(ch = getchar()); res = (res << 1) + (res << 3) + numm);
flag && (res = -res);
}
template<typename T>
void write(T x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
//static auto _ = []() {
// ios::sync_with_stdio(false);
// cin.tie(0);
// return 0;
//}();
//#pragma GCC optimize(3,"Ofast","inline")
//
const int inf = 0x3f3f3f3f;
int t, k, sum, mp[5][5];
int rote(int x, int y) {
swap(mp[x][y], mp[x + 1][y]);
swap(mp[x][y + 1], mp[x + 1][y + 1]);
swap(mp[x][y], mp[x + 1][y + 1]);
return mp[x][y] + mp[x + 1][y] + mp[x][y + 1] + mp[x + 1][y + 1];
}
void rerote(int x, int y) {
swap(mp[x][y], mp[x + 1][y + 1]);
swap(mp[x][y + 1], mp[x + 1][y + 1]);
swap(mp[x][y], mp[x + 1][y]);
}
int dfs(int sum, int dep, int alpha, int beta) {
if (dep == 2 * k) return sum;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int tmp = dfs(sum + rote(i, j), dep + 1, alpha, beta);
rerote(i, j);
if (dep & 1)
beta = min(beta, tmp);
else
alpha = max(alpha, tmp);
if(beta <= alpha) break;
}
if(beta <= alpha) break;
}
return dep & 1 ? beta : alpha;
}
int main() {
read(t);
while(t--) {
read(k);
sum = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
read(mp[i][j]);
}
}
int ans = dfs(sum, 0, -inf, inf);
write(ans), pn;
}
return 0;
}