https://ac.nowcoder.com/acm/contest/32183/A
一个很有意思的搜索,先手希望结果尽可能的大,后手希望结果尽可能的小。所以在枚举的时候,先后手的策略是不一样的。
#include <bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
using ldb = long double;
const i32 inf = INT_MAX / 2;
const i64 INF = LLONG_MAX / 2;
#define int i64
using vi = vector<int>;
int a[4][4], k;
int query(int x, int y) {
return a[x][y] + a[x + 1][y] + a[x][y + 1] + a[x + 1][y + 1];
}
void change1(int x, int y) {
swap(a[x][y], a[x][y + 1]);
swap(a[x][y + 1], a[x + 1][y + 1]);
swap(a[x + 1][y], a[x + 1][y + 1]);
}
void change2(int x, int y) {
swap(a[x][y], a[x][y + 1]);
swap(a[x][y], a[x + 1][y]);
swap(a[x + 1][y], a[x + 1][y + 1]);
}
int dfs(int i) {
if (i == k) return 0;
if (i & 1) {
int res = inf;
for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++) {
change1(x, y);
res = min(res, dfs(i + 1) + query(x, y));
change2(x, y);
}
return res;
} else {
int res = -inf;
for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++) {
change1(x, y);
res = max(res, dfs(i + 1) + query(x, y));
change2(x, y);
}
return res;
}
}
void solve() {
cin >> k, k *= 2;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
cin >> a[i][j];
cout << dfs(0) << "\n";
}
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int T;
cin >> T;
while (T--)
solve();
return 0;
}
标签:return,int,res,Nanning,Rake,++,swap,using,2017
From: https://www.cnblogs.com/PHarr/p/18387485