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. Vue.extend构造器和$mount实例构造组件后可以用$destroy()进行卸载,$forceUpdate()进行更新,$nextTick()数据修改

    html <div id="app"> </div> <p><button onclick="destroy()"&g ...

  2. Derek解读Bytom源码-孤块管理

    作者:Derek 简介 Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom ...

  3. 忘记mysql密码处理方案

    https://blog.csdn.net/lisongjia123/article/details/57418989 http://blog.51cto.com/lxsym/477027

  4. Hexo搭建(VPS)

    都说 hexo 是静态的 Blog,当时不明觉厉= =.后来终于知道了什么意思......所谓的静态,其实就是因为你不能改云端,而是依赖本地数据,然后使用命令将本地数据变成 web 数据再使用浏览器进 ...

  5. BZOJ 2809: [Apio2012]dispatching(左偏树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2809 题意: 思路:最简单的想法就是枚举管理者,在其子树中从薪水低的开始选起,但是每个节点都这样处理 ...

  6. JQGrid 在页面加载时展开SubGrid

    1.jqgrid在加载完成之后展开SubGrid,用得如下办法: gridComplete: function () { var rowIds = $("#list2").getD ...

  7. 用命令bat打开某个文件或文件夹

    打开文件或文件夹可以用start命令,start命令会根据文件关联的程序自动调用关联的程序打开文件和文件夹. 可以用记事本写如下命令,之后改扩展名为.bat即可 打开文件夹 start "& ...

  8. vs编译出现 fatal error LNK1281:无法生成 SAFESEH 映像

    问题: 在vs编译中我们有时候常常会见到这样的错误,无法生成 SAFESEH 映像,镜像安全问题 解决方法: 1.打开该项目的"属性页"对话框. 2.单击"链接器&quo ...

  9. 百度“搜索设置”之基于定位下拉框或者需要点击link才显示的下拉框,二次定位与多次定位实现的实际效果区别

    还是基于上次那个练习的后续出现的思考,http://www.cnblogs.com/8013-cmf/p/6555790.html 界面: 源码: 写法如下:  继续解释这两种的区别: 1.其实基于定 ...

  10. modal结合art-template

    内容div <div id="modal-cont"></div> 模板tpl <script id="modal-tpl" ty ...