[2017 ICPC Nanning] Rake It In

PHarr發表於2024-08-29

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;
}

相關文章