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.

样例输入

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

之前搜有大佬说需要用到alpha-beta剪枝,但是数据范围非常小,似乎不需要

还是贴个链接吧,如果有需要的可以看看https://blog.csdn.net/qq_27008079/article/details/60869054

暴力DFS,回溯,感觉没太多好讲的

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi=acos(-);
const int mod=1e9+;
const int inf=<<;
const int maxn=1e5+;
int a[][];
int k;
int val(int i,int j){
return a[i][j]+a[i+][j]+a[i][j+]+a[i+][j+];
}
void rotat(int i,int j){
int a1=a[i][j],a2=a[i][j+],a3=a[i+][j],a4=a[i+][j+];
a[i][j]=a2,a[i][j+]=a4,a[i+][j]=a1,a[i+][j+]=a3;
}
void retat(int i,int j){
int a1=a[i][j],a2=a[i][j+],a3=a[i+][j],a4=a[i+][j+];
a[i][j]=a3,a[i][j+]=a1,a[i+][j]=a4,a[i+][j+]=a2;
}
int dfs(int t){
int ans;
if(t==*k-){
ans=inf;
for(int i=;i<;i++)
for(int j=;j<;j++)
ans=min(ans,val(i,j));
return ans;
}
else if(t%==){
ans=;
for(int i=;i<;i++)
for(int j=;j<;j++){
rotat(i,j);
int tmp=val(i,j);
ans=max(ans,tmp+dfs(t+));
retat(i,j);
}
}
else{
ans=inf;
for(int i=;i<;i++)
for(int j=;j<;j++){
rotat(i,j);
int tmp=val(i,j);
ans=min(ans,tmp+dfs(t+));
retat(i,j);
}
}
return ans;
}
int main(){
int T;scanf("%d",&T);
while(T--){
scanf("%d",&k);
for(int i=;i<;i++){
for(int j=;j<;j++){
scanf("%d",&a[i][j]);
}
}
cout<<dfs()<<endl;
}
return ;
}

DFS CCPC2017 南宁I题的更多相关文章

  1. hdu 1312:Red and Black(DFS搜索,入门题)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. Codeforces Gym 100338I TV Show 傻逼DFS,傻逼题

    Problem I. TV ShowTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...

  3. 2017ICPC南宁 M题 The Maximum Unreachable Node Set【二分图】

    题意: 找出不能相互访问的点集的集合的元素数量. 思路: 偏序集最长反链裸题. 代码: #include<iostream> #include<cstring> using n ...

  4. hdu1242 Rescue DFS(路径探索题)

    这里我定义的路径探索题指 找某路能够到达目的地,每次走都有方向,由于是探索性的走 之后要后退 那些走过的状态都还原掉 地址:http://acm.hdu.edu.cn/showproblem.php? ...

  5. 湖南省第六届省赛题 Biggest Number (dfs+bfs,好题)

    Biggest Number 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 You have a maze with obstacles and non-zero di ...

  6. hdu 2553:N皇后问题(DFS遍历,水题)

    N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. zzulioj--1730--通信基站(全排列+dfs)(好题)

    1730: 通信基站 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 28  Solved: 11 SubmitStatusWeb Board Desc ...

  8. 1506 传话 (暴力DFS或者Tarjan模板题)

    题目描述 Description 一个朋友网络,如果a认识b,那么如果a第一次收到某个消息,那么会把这个消息传给b,以及所有a认识的人. 如果a认识b,b不一定认识a. 所有人从1到n编号,给出所有“ ...

  9. 【LeetCode】深搜DFS(共85题)

    [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...

随机推荐

  1. HihoCoder 1636 Pangu and Stones(区间DP)题解

    题意:合并石子,每次只能合并l~r堆成1堆,代价是新石堆石子个数,问最后能不能合成1堆,不能输出0,能输出最小代价 思路:dp[l][r][t]表示把l到r的石堆合并成t需要的最小代价. 当t == ...

  2. (转)Shiro学习

    (二期)13.权限框架shiro讲解 [课程13]自定义Realm.xmind36.8KB [课程13]用户授权流程.xmind0.2MB [课程13]shiro简介.xmind0.3MB [课程13 ...

  3. Kibana --> Getting Started -->Building your own dashboard

    https://www.elastic.co/guide/en/kibana/6.6/tutorial-build-dashboard.html Building your own dashboard ...

  4. 编译 glibc-2.14 时出现的一个LD_LIBRARY_PATH不路径bug

    ../configure --prefix=/home/zzhy/wd/software/glibc-2.14 错误:checking LD_LIBRARY_PATH variable... cont ...

  5. docker的安装和使用

    docker在linux上安装我尝试了几次,都报错了,看到其它人的博客说明这也确实是个问题. 后来在朋友给安装了一个VMware虚拟机后,很方便地就在里面进行了安装. 概念理解: 仓库: 别人做好的现 ...

  6. JavaScript 拥有动态类型

    字符串.数字.布尔.数组.对象.Null.Undefined JavaScript 拥有动态类型 JavaScript 拥有动态类型.这意味着相同的变量可用作不同的类型: 实例 var x // x ...

  7. Transaction

    SqlTransaction——事务详解 事务是将一系列操作作为一个单元执行,要么成功,要么失败,回滚到最初状态.在事务处理术语中,事务要么提交,要么中止.若要提交事务,所有参与者都必须保证对数据的任 ...

  8. strlen函数,strcat函数,strcpy函数,strncpy函数,strcmp函数

    strcpy函数: char *strcpy(char *Dest , const char *Src) { assert((Dest != NULL) && (Src != NULL ...

  9. C#解析html文档类库HtmlAgilityPack下载地址

    新:http://html-agility-pack.net/?z=codeplex 原:http://htmlagilitypack.codeplex.com/

  10. python shelve模块

    #coding=utf- import shelve f = shelve.open("shelve_test") f['info'] = "alex" f[, ...