显示原始代码
#include <bits/stdc++.h>
using namespace std;
int a[4][4];
int temps[1000000];
int coun = 0;
int lll;
int left(int x2) {
int xx = x2 % 3;
int yy = x2 / 3;
int temp = a[xx][yy];
a[xx][yy] = a[xx + 1][yy];
a[xx + 1][yy] = a[xx + 1][yy + 1];
a[xx + 1][yy + 1] = a[xx][yy + 1];
a[xx][yy + 1] = temp;
int u = (a[xx][yy] + a[xx + 1][yy] + a[xx][yy + 1] + a[xx + 1][yy + 1]);
return u;
}
int right(int x) {
int xx = x % 3;
int yy = x / 3;
int temp = a[xx][yy];
a[xx][yy] = a[xx][yy + 1];
a[xx][yy + 1] = a[xx + 1][yy + 1];
a[xx + 1][yy + 1] = a[xx + 1][yy];
a[xx + 1][yy] = temp;
int u = (a[xx][yy] + a[xx + 1][yy] + a[xx][yy + 1] + a[xx + 1][yy + 1]);
return u;
}
int dfs(int x, int m, int count) {
int temp = 0;
temp = count;
if (m == 0) {
temps[coun] = temp;
coun++;
return temp;
} else
int temp = 0;
for (int i = 0; i < 9; i++) {
temp += right(i);
dfs(i, m - 1, temp);
temp -= left(i);
}
}
int select(int m) {
if (m <= 0) {
return temps[0];
}
for (int p = 0; p < pow(9, m - 1); p++) {
temps[p] = temps[9 * p];
for (int i = 0; i < 9; i++) {
int a = temps[p];
int b = temps[p * 9 + i];
temps[p] = a < b ? a : b;
}
}
for (int p = 0; p < pow(9, m - 2); p++) {
temps[p] = temps[9 * p];
for (int i = 0; i < 9; i++) {
int a = temps[p];
int b = temps[p * 9 + i];
temps[p] = a > b ? a : b;
}
}
return select(m - 2);
}
int main() {
int n, lll;
cin >> n;
while (n--) {
cin >> lll;
coun = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cin >> a[i][j];
}
}
dfs(0, 2 * lll, 0);
cout << select(2 * lll) << endl;
}
}