[JSOI2018] D1T1 chess [对抗搜索][状压DP]

本文介绍了一种基于棋盘游戏的算法实现,通过C++代码详细展示了如何利用哈希表和深度优先搜索来解决游戏中的最优策略问题。文章涵盖了状态表示、哈希函数的设计以及递归搜索策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * @Author: SugarSBN
 * @Date: 2018-05-21 21:54:00
 * @Language:   C++
 * @Task: chess
 */
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
#include<fstream>
using namespace std;
const int MAXN=1e5+11;
const double pi=acos(-1);
const int inf = 2e7;
const int base = 12;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef double dbl;
#define For(a,b,c) for (int (a)=(b);(a)<(c);(a)++)
#define foreach(iter,V) for (__typeof((V.begin()) iter=(V).begin();iter!=(V).end();iter++)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
int n, m;
int a[15][15], b[15][15], hei[15];
map<ll, int> mapp;
ll hashs(){
    ll res = 0;
    For(i, 1, n + 1) res = res * base + hei[i];
    return res;
}
void unzip(ll x){
    for (int i = n; i ; i--)  {
        hei[i] = x % base;
        x /= base;
    }
}
bool maxn_or_minn(){
    ll res = 0;
    For(i, 1, n + 1) res += hei[i];
    return res & 1;
}
int dfs(ll sta){
    if (mapp.find(sta)!=mapp.end())
        return mapp[sta];
    unzip(sta);
    bool tpe = maxn_or_minn();
    int ret = tpe ? inf : -inf;
    For(i, 1, n + 1)
        if (hei[i - 1] > hei[i]){
            ++hei[i];
            ll h = hashs();
            if (!tpe)
                ret = max(ret, dfs(h) + a[i][hei[i]]);
            else
                ret = min(ret, dfs(h) - b[i][hei[i]]);
            --hei[i];
        }
        return mapp[sta] = ret;
}
int main(){
    scanf("%d%d", &n, &m);
    hei[0] = m;
    For(i, 0, n)
        For(j, 0, m) scanf("%d", &a[i+1][j+1]);
    For(i, 0, n)
        For(j, 0, m) scanf("%d", &b[i+1][j+1]);
    For(i, 1, n + 1) hei[i] = m;
    ll full = hashs();
    mapp[full] = 0;
    dfs(0);
    printf("%d\n", mapp[0]);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值