[模板] Alpha-Beta 剪枝

题源: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值