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;
    }
    

      

  • 相关阅读:
    Java Volatile keyword
    解决 The &#39;InnoDB&#39; feature is disabled; you need MySQL built with &#39;InnoDB&#39; to have it working
    【玩转cocos2d-x之三十九】Cocos2d-x 3.0截屏功能集成
    【DP】UVA 624 CD 记录路径
    ns3加入模块之vanet-highway
    awk向脚本传递參数(二)
    【传递正能量】献给默默追梦的人
    算法(第四版)学习笔记之java实现可以动态调整数组大小的栈
    Webstorm/IntelliJ Idea 过期破解方法
    CenterOS下安装NodeJS
  • 原文地址:https://www.cnblogs.com/zyf3855923/p/8763325.html
Copyright © 2011-2022 走看看