zoukankan      html  css  js  c++  java
  • BZOJ 5248: [2018多省省队联测]一双木棋(对抗搜索)

    Time Limit: 20 Sec  Memory Limit: 512 MB
    Submit: 439  Solved: 379
    [Submit][Status][Discuss]

    Description

    菲菲和牛牛在一块n行m列的棋盘上下棋,菲菲执黑棋先手,牛牛执白棋后手。棋局开始时,棋盘上没有任何棋子,
    两人轮流在格子上落子,直到填满棋盘时结束。落子的规则是:一个格子可以落子当且仅当这个格子内没有棋子且
    这个格子的左侧及上方的所有格子内都有棋子。
    棋盘的每个格子上,都写有两个非负整数,从上到下第i行中从左到右第j列的格子上的两个整数记作Aij、Bij。在
    游戏结束后,菲菲和牛牛会分别计算自己的得分:菲菲的得分是所有有黑棋的格子上的Aij之和,牛牛的得分是所
    有有白棋的格子上的Bij的和。
    菲菲和牛牛都希望,自己的得分减去对方的得分得到的结果最大。现在他们想知道,在给定的棋盘上,如果双方都
    采用最优策略且知道对方会采用最优策略,那么,最终的结果如何
     
     

    Input

    第一行包含两个正整数n,m,保证n,m≤10。
    接下来n行,每行m个非负整数,按从上到下从左到右的顺序描述每个格子上的
    第一个非负整数:其中第i行中第j个数表示Aij。
    接下来n行,每行m个非负整数,按从上到下从左到右的顺序描述每个格子上的
    第二个非负整数:其中第i行中第j个数表示Bij
    n, m ≤ 10 , Aij, Bij ≤ 100000
     

    Output

    输出一个整数,表示菲菲的得分减去牛牛的得分的结果。
     

    Sample Input

    2 3
    2 7 3
    9 1 2
    3 7 2
    2 3 1

    Sample Output

    2

    HINT

     

    Source

    用$now[i]$表示第$i$行已经放了$now[i]$个棋子

    爆搜之后发现状态数很小

    因此用map记忆化一下就好

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<map>
    #include<ext/pb_ds/assoc_container.hpp>
    #include<ext/pb_ds/hash_policy.hpp>
    #define LL long long 
    using namespace std;
    using namespace __gnu_pbds;
    const int MAXN = 11, INF = 1e9 + 10, base = 12;
    inline int read() {
        char c = getchar(); int x = 0, f = 1; 
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x * f;
    }
    int N, M;
    int A[MAXN][MAXN], B[MAXN][MAXN];
    cc_hash_table<LL, int>mp;
    int now[MAXN];
    LL gethash() {
        LL x = 0;
        for(int i = 1; i <= N; i++) x = x * base + now[i];
        return x;
    }
    void push(LL x) {
        for(int i = N; i >= 1; i--) now[i] = x % base, x /= base;
    }
    int getnext() {
        int x = 0;
        for(int i = 1; i <= N; i++) x += now[i];
        return x & 1;
    }
    int dfs(LL sta) {
        if(mp.find(sta) != mp.end()) return mp[sta];
        push(sta);
        bool type = getnext();
        int ans = !type ? -INF : INF;
        for(int i = 1; i <= N; i++) {
            if(now[i] < now[i - 1]) {
                now[i]++;
                LL nxt = gethash();
                int val = dfs(nxt);
                ans = !type ? max(ans, val + A[i][now[i]]) : min(ans, val - B[i][now[i]]);
                now[i]--;
            }
        }
        return mp[sta] = ans;
    }
    main() {
        #ifdef WIN32
        freopen("a.in", "r", stdin);
        //freopen("a.out", "w", stdout);
        #else
        freopen("chess2018.in", "r", stdin);
        freopen("chess2018.out", "w", stdout);
        #endif
        N = read(); M = read();
        for(int i = 1; i <= N; i++)
            for(int j = 1; j <= M; j++)
                A[i][j] = read();
        for(int i = 1; i <= N; i++)
            for(int j = 1; j <= M; j++)
                B[i][j] = read();
        for(int i = 0; i <= N; i++) now[i] = M;
        mp[gethash()] = 0;
        dfs(0);
        printf("%d", mp[0]);
    }
  • 相关阅读:
    1007 Maximum Subsequence Sum(25 分)
    1006 Sign In and Sign Out(25 分)
    1005 Spell It Right
    1004 Counting Leaves
    Struts10分钟入门
    MyBais入门
    Hibernate注解
    save,flush,evict
    HQL连接查询
    Hibernate-延迟加载和立即加载
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/9061269.html
Copyright © 2011-2022 走看看