zoukankan      html  css  js  c++  java
  • <搜索> Rake It In ICPC 2017 Nanning

    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 × 4 board is created with 16 values placed on the board.
    Starting with player Alice, each player in a round selects a 2 × 2 region of the board, adding the sum of values in the region to the score indicator, and then rotating these four values 90 degrees counterclockwise.
    After 2k 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.

    输入

    The input contains several test cases and the first line provides an integer t (1 ≤ t ≤ 200) which is the number of test cases.
    Each case contains five lines. The first line provides the integer 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 1 to 10.

    输出

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

    样例输入

    4
    1
    1 1 2 2
    1 1 2 2
    3 3 4 4
    3 3 4 4
    2
    1 2 3 4
    1 2 3 4
    1 2 3 4
    1 2 3 4
    3
    1 1 4 4
    4 4 1 1
    1 1 4 4
    1 4 1 4
    3
    1 2 3 4
    5 1 2 3
    4 5 1 2
    3 4 5 1
    

    样例输出

    20
    40
    63
    71

    不难想到是搜索,但这样有先后手的搜索,还是第一次遇到。而且,这样搜索里带上下界记录答案的方法,也要记录下来...
    #include <iostream>
    #include <bits/stdc++.h>
    using namespace std;
    int a[6][6]={0};
    int sum=0;
    int k;
    void swaparray(int x,int y)
    {
        int temp=a[x][y];
        a[x][y]=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]=temp;
        sum+=a[x][y]+a[x][y+1]+a[x+1][y]+a[x+1][y+1];
    }
    void restorearray(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;
        sum-=a[x][y]+a[x][y+1]+a[x+1][y]+a[x+1][y+1];
    }
     
    int dfs_max(int n,int s1,int s2);
    int dfs_min(int n,int s1,int s2);
    int dfs_min(int n,int s1,int s2)
    {
        if(n==k)
        {
            return sum;
        }
        int sum1=0,sum2=0;
        int i,j,xx,yy;
        for(i=1;i<=3;i++)
        {
            for(j=1;j<=3;j++)
            {
                swaparray(i,j);
                int val=dfs_max(n+1,s1,s2);
                restorearray(i,j);
                if(val<=s1)
                    return s1;
                if(val<s2)
                    s2=val;
            }
        }
        return s2;
    }
    int dfs_max(int n,int s1,int s2)
    {
        if(n==k)
        {
            return sum;
        }
        int i,j,xx,yy;
        for(i=1;i<=3;i++)
        {
            for(j=1;j<=3;j++)
            {
                swaparray(i,j);
                int val=dfs_min(n+1,s1,s2);
                restorearray(i,j);
                if(val>=s2)
                    return s2;
                if(val>s1)
                    s1=val;
            }
        }
        return s1;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&k);
            k*=2;
            int i,j;
            for(i=1;i<=4;i++)
            {
                for(j=1;j<=4;j++)
                {
                    scanf("%d",&a[i][j]);
                }
            }
            int ans=dfs_max(0,0,10000000);
            printf("%d
    ",ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    快速幂&欧拉降幂
    欧拉函数
    素数打表-筛法
    多源最短路径问题(Floyd算法)
    蓝桥杯-本质上升序列
    蓝桥杯-玩具蛇
    SDUT-计算机组成原理
    取消U盘和移动硬盘的GPT保护分区
    Windows 10 上运行 photoshop 等软件出现 loadlibrary failed with error 87 的解决方法!
    做题笔记
  • 原文地址:https://www.cnblogs.com/zyf3855923/p/8763325.html
Copyright © 2011-2022 走看看