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

      

  • 相关阅读:
    三元表达式 列表和字典推导式 函数对象 名称空间 作用域 global和nonlocal 函数装饰器 枚举对象
    函数参数 打散机制 字符串比较 返回值
    函数简介
    三种字符串的介绍 文件的读写
    字符编码
    数据类型及其常用方法 数据类型转换 可变与不可变 值拷贝与深浅拷贝
    流程控制 while和for循环
    变量命名规范 常量 输入和输出 注释 数据类型 运算符 逻辑运算符
    语言分类 编译型和解释型语言分析 环境变量 代码执行的方式 pip介绍 变量
    Python django tests
  • 原文地址:https://www.cnblogs.com/zyf3855923/p/8763325.html
Copyright © 2011-2022 走看看