题目链接:Rake It In

比赛链接:ICPC Asia Nanning 2017

Description

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 4\) board is created with 16 values placed on the board. Starting with player Alice, each player in a round selects a \(2 \times 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.

Input

The input contains several test cases and the first line provides an integer \(t (1 \le t \le 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)\). Each of the following four lines contains four integers indicating the values on the board initially. All values are integers between \(1\) to \(10\).

Output

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

Sample Input

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

Sample Output

20
40
63
71

Solution

题意

有一块 \(4\times 4\) 的板,Alice 和 Bob 每次选择 \(2\times 2\) 的区域并逆时针旋转 \(90\) 度,这个区域的和累加到总分上。现在 Alice 先手,有 \(k\) 轮游戏,Alice 想要分数最大化,Bob 想要分数最小化,求最终的分数。

题解

DFS 贪心

比较好的解法是对抗搜索 与 \(Alpha-Beta\) 剪枝。

题解给出是上分支定界和启发式搜索。

但是用贪心 + 爆搜竟然过了。

关于对抗搜索和 \(Alpha-Beta\) 剪枝以后再更新。

Code

DFS + 贪心

#include <bits/stdc++.h>
using namespace std;
const int inf = 1000; int k;
int mt[10][10]; // 交换两数
void swap(int &a, int &b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
} // 逆时针旋转
void rote(int x, int y) {
swap(mt[x][y], mt[x + 1][y]);
swap(mt[x][y + 1], mt[x + 1][y + 1]);
swap(mt[x][y], mt[x + 1][y + 1]);
} // 顺时针旋转
void rerote(int x, int y) {
swap(mt[x][y], mt[x + 1][y + 1]);
swap(mt[x][y + 1], mt[x + 1][y + 1]);
swap(mt[x][y], mt[x + 1][y]);
} // 求和
int sum(int x, int y) {
return mt[x][y] + mt[x + 1][y] + mt[x][y + 1] + mt[x + 1][y + 1];
} int dfs(int step) {
if(step == 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 = (step & 1)? 0: inf;
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
rote(i, j); // 逆时针旋转
if(step & 1) {
ans = max(ans, sum(i, j) + dfs(step + 1));
} else {
ans = min(ans, sum(i, j) + dfs(step + 1));
}
rerote(i, j); // 回溯时转回来
}
}
return ans;
}
} int main() {
int T;
cin >> T;
while(T--) {
scanf("%d", &k);
for(int i = 0; i < 4; ++i) {
for(int j = 0; j < 4; ++j) {
scanf("%d", &mt[i][j]);
}
}
int ans = dfs(1);
printf("%d\n", ans);
}
return 0;
}

对抗搜索 + \(Alpha-Beta\) 剪枝

#include <bits/stdc++.h>
using namespace std;
const int inf = 1000; int k;
int mt[10][10]; void swap(int &a, int &b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
} void rote(int x, int y) {
swap(mt[x][y], mt[x + 1][y]);
swap(mt[x][y + 1], mt[x + 1][y + 1]);
swap(mt[x][y], mt[x + 1][y + 1]);
} void rerote(int x, int y) {
swap(mt[x][y], mt[x + 1][y + 1]);
swap(mt[x][y + 1], mt[x + 1][y + 1]);
swap(mt[x][y], mt[x + 1][y]);
} int cnt(int x, int y) {
return mt[x][y] + mt[x + 1][y] + mt[x][y + 1] + mt[x + 1][y + 1];
} int dfs(int sum, int step, int alpha, int beta) {
if(step == 2 * k + 1) {
return sum;
} else {
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
rote(i, j);
if(step & 1) {
alpha = max(alpha, dfs(sum + cnt(i, j), step + 1, alpha, beta));
} else {
beta = min(beta, dfs(sum + cnt(i, j), step + 1, alpha, beta));
}
rerote(i, j);
if(beta <= alpha) break;
}
if(beta <= alpha) break;
}
return (step & 1)? alpha: beta;
}
} int main() {
int T;
cin >> T;
while(T--) {
scanf("%d", &k);
for(int i = 0; i < 4; ++i) {
for(int j = 0; j < 4; ++j) {
scanf("%d", &mt[i][j]);
}
}
int ans = dfs(0, 1, -inf, inf);
printf("%d\n", ans);
}
return 0;
}

ICPC Asia Nanning 2017 I. Rake It In (DFS+贪心 或 对抗搜索+Alpha-Beta剪枝)的更多相关文章

  1. ICPC Asia Nanning 2017 L. Twice Equation (规律 高精度运算)

    题目链接:Twice Equation 比赛链接:ICPC Asia Nanning 2017 Description For given \(L\), find the smallest \(n\) ...

  2. ICPC Asia Nanning 2017 F. The Chosen One (高精度运算)

    题目链接:The Chosen One 比赛链接:ICPC Asia Nanning 2017 题意 \(t\) 组样例,每组给出一个整数 \(n(2\le n\le 10^{50})\),求不大于 ...

  3. ICPC Asia Nanning 2017 F. The Chosen One (大数、规律、2的k次幂)

    Welcome to the 2017 ACM-ICPC Asia Nanning Regional Contest.Here is a breaking news. Now you have a c ...

  4. 2016 ACM/ICPC Asia Regional Dalian Online 1010 Weak Pair dfs序+分块

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...

  5. 2020 ICPC Asia Taipei-Hsinchu Regional Problem B Make Numbers (dfs搜索)

    题意:给你四个数字,你可以用这四个数字凑出四个1位数,一个2位数和两个1位数,或一个3位数和一个1位数,你可以用你凑出的数字进行\(+,-,x\)运算(所有运算符号至少出现一次),问你一共能得到多少个 ...

  6. 2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

  7. 2017 ACM ICPC Asia Regional - Daejeon

    2017 ACM ICPC Asia Regional - Daejeon Problem A Broadcast Stations 题目描述:给出一棵树,每一个点有一个辐射距离\(p_i\)(待确定 ...

  8. 2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest

    2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest A - Arranging Wine 题目描述:有\(R\)个红箱和\(W\)个白箱,将这 ...

  9. 2017 ACM/ICPC Asia Regional Qingdao Online

    Apple Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submi ...

随机推荐

  1. Normal Equation Algorithm

    和梯度下降法一样,Normal Equation(正规方程法)算法也是一种线性回归算法(Linear Regression Algorithm).与梯度下降法通过一步步计算来逐步靠近最佳θ值不同,No ...

  2. C#变量1

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 1.变量:代表这内存(RAM,保存正在运行程序的数据,断电RAM中的数据将会丢失)中的一块空间,我们可以通过变量的名称存/取数据, 因此我 ...

  3. QT的三种开发方式

    最近在学习QT GUI,单纯使用C++硬编码的方式,直接是采用QWidget部件来做,而不是采用QT Designer做UI界面,也不是采用QML+Javascript.单纯使用C++硬编码的方式,缺 ...

  4. swoole安装异步reids

    /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config --enable-async ...

  5. # Python第十节 传参

    Python第十节 传参 一. 变量和变量名 首先说明变量名和变量的一点差异 例如: var = [1, 2, 3] `var = "Google" 调用变量var的时候, 既可以 ...

  6. 将 XML 架构(XSD)附加到Word文档

    附加到文档中的 XML 架构是为您的组织进行自定义而设计的.XML 架构通常由 IT 专业人员创建,他们的职责就是在 Word 中为您的组织构建专用的模板或解决方案. 可用于附加到文档的架构在架构库中 ...

  7. ASP.NET MVC 学习笔记之面向切面编程与过滤器

    AOP(面向切面)是一种架构思想,用于把公共的逻辑放到一个单独的地方,这样就不用每个地方都写重复的代码了.比如程序中发生异常,不用每个地方都try…catch 只要在Golbal的Applicatio ...

  8. 2019-9-2-win10-uwp-Markdown

    title author date CreateTime categories win10 uwp Markdown lindexi 2019-09-02 12:57:38 +0800 2018-2- ...

  9. Spring集成RMI实现远程调用

    前提: 1.开发工具: jdk tomcat ecplise,开发工具的使用本篇不做介绍. 2.需具备以下知识:javase servelt web rmi spring maven 一.关于RMI ...

  10. VUE.JS 环境配置

    首先安装   node.js 网址 https://nodejs.org/en/ 选择版本 点击直接安装OK  (不用安装到系统盘) 然后cmd 命令框 输入 npm -version (查看安装版本 ...