ACM-ICPC 2017 Asia Nanning I.Rake It In (对抗搜索)

题目链接
The designers have come up with a new simple game called “Rake It In”. Two players, Alice and Bob, initially select an integer k and initialize a score indicator. An 4 \times 44×4 board is created with 16 values placed on the board. Starting with player Alice, each player in a round selects a 2 \times 22×2 region of the board, adding the sum of values in the region to the score indicator, and then rotating these four values 9090 degrees counterclockwise.

After 22k rounds in total, each player has made decision in k times. The ultimate goal of Alice is to maximize the final score. However for Bob, his goal is to minimize the final score.

In order to test how good this game is, you are hired to write a program which can play the game. Specifically, given the starting configuration, they would like a program to determine the final score when both players are entirely rational.

Input
The input contains several test cases and the first line provides an integer t (1 \le t \le 200)t(1≤t≤200) which is the number of test cases.

Each case contains five lines. The first line provides the integer k (1 \le k \le 3)k(1≤k≤3). Each of the following four lines contains four integers indicating the values on the board initially. All values are integers between 11 to 1010.

Output
For each case, output an integer in a line which is the predicted final score.


这道题是南宁区域赛的铜到铁一道题,题意就是两个人玩游戏,每个人选一个2x2的区域,选择之后逆时针旋转一下这个区域。先手选最大的,后手选最小的,然后让你求出来两个人都采用最佳策略下的分数。
这道题当场因为一些非常SB的原因没有做出来,今天花了一点时间重写了一下,没想到一发就过了,很不开心……。
其实做法很简单,我们把这个搜索树按照层数分成两个节点层,奇数层是求最大值的节点(先手),偶数层就是求最小值的节点(后手),然后暴力找左右的可行位置,按照节点的属性更新答案。因为层数是固定的,所以就不需要在这棵树上剪枝了。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
int a[10][10];
int k;
void rev(int x,int y)
{
    int temp = a[x][y+1];
    a[x][y+1] = a[x+1][y+1];
    a[x+1][y+1] = a[x+1][y];
    a[x+1][y] = a[x][y];
    a[x][y] = temp;
}
void revrev(int x,int y)
{
    int temp = a[x][y];
    a[x][y] = a[x+1][y];
    a[x+1][y] = a[x+1][y+1];
    a[x+1][y+1] = a[x][y+1];
    a[x][y+1] = temp;
}
int sum(int x,int y)
{
    return a[x][y] + a[x+1][y] + a[x][y+1] + a[x+1][y+1];
}
int dfs(int pos)
{
    if(pos == 2*k)
    {
        int ans = INF;
        for(int i = 0;i<3;i++)
        {
            for(int j = 0;j<3;j++)
            {
                ans = min(ans,sum(i,j));
            }
        }
        return ans;
    }
    else
    {
        int ans = ((pos & 1 ) ? 0 : INF);
        for(int i = 0;i<3;i++)
        {
            for(int j = 0;j<3;j++)
            {
                rev(i,j);
                if(pos & 1) ans = max(ans,sum(i,j)+dfs(pos+1));
                else ans = min(ans,sum(i,j)+dfs(pos+1));
                revrev(i,j);
            }
        }
        return ans;
    }
}
int main()
{
    int ca;
    scanf("%d",&ca);
    while(ca--)
    {
        scanf("%d",&k);
        for(int i = 0;i<4;i++)
        {
            for(int j = 0;j<4;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        int ans = dfs(1);
        printf("%d\n",ans);
    }
}

猜你喜欢

转载自blog.csdn.net/lingzidong/article/details/80356194