显示原始代码
#include <bits/stdc++.h>
using namespace std;
int k, tip[4][4];
void oprea(int starta, int startb) {
int pos1 = tip[starta][startb], pos2 = tip[starta][startb + 1], pos3 = tip[starta + 1][startb],
pos4 = tip[starta + 1][startb + 1];
tip[starta][startb] = pos3;
tip[starta][startb + 1] = pos1;
tip[starta + 1][startb] = pos4;
tip[starta + 1][startb + 1] = pos2;
}
void opreb(int starta, int startb) {
int pos1 = tip[starta][startb], pos2 = tip[starta][startb + 1], pos3 = tip[starta + 1][startb],
pos4 = tip[starta + 1][startb + 1];
tip[starta][startb] = pos2;
tip[starta][startb + 1] = pos4;
tip[starta + 1][startb] = pos1;
tip[starta + 1][startb + 1] = pos3;
}
int dfs2(int d);
int dfs1(int d) {
if (d == k)
return 0;
int ans = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
opreb(i, j);
ans = max(ans, dfs2(d) + tip[i][j] + tip[i + 1][j] + tip[i + 1][j + 1] + tip[i][j + 1]);
oprea(i, j);
}
return ans;
}
int dfs2(int d) {
int ans = 1000000000;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
opreb(i, j);
ans = min(ans, dfs1(d + 1) + tip[i][j] + tip[i + 1][j] + tip[i + 1][j + 1] + tip[i][j + 1]);
oprea(i, j);
}
return ans;
}
void solve() {
scanf("%d", &k);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++) scanf("%d", &tip[i][j]);
printf("%d\n", dfs1(0));
}
int main() {
int T;
scanf("%d", &T);
while (T > 0) {
solve();
T -= 1;
};
return 0;
}