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. addEventListener在一个节点上添加多个相同的事件

    <button id='btn'>attachEvent</button> window.onload=function(){ var Obtn = document.getE ...

  2. [exceltolist] - 一个excel转list的工具

    https://github.com/deadzq/cp-utils-excelreader  <(感谢知名网友的帮助) https://sargeraswang.com/blog/2018/1 ...

  3. EPPlus实战篇——Excel读取

    .net core 项目 可以从excel读取任何类型(T)的数据,只要T中的field的[Display(Name = "1233")]中的name==excel column ...

  4. FPGA 概述2

    参考1 参考2:浅论各种调试接口(SWD.JTAG.Jlink.Ulink.STlink)的区别 以下数据仅供参考 文章概要 主流FPGA厂商及产品 相同设计在FPGA与ASIC中耗费器件数量比较 F ...

  5. 1、HA Cluster基础原理

    Linux Cluster  -->  linux集群类型分三种: LB:负载均衡,LoadBalance HA:双机集群系统,指高可用性集群,High Available HP:Hadoop ...

  6. WebPack基本概念以及基本使用

    入门Webpack,看这篇就够了 2017年9月18日更新,添加了一个使用webpack配置多页应用的demo,可以点击此处查看 2017年8月13日更新,本文依据webpack3.5.3将文章涉及代 ...

  7. python,函数的基本用法

    一.函数 函数的概念:对功能或者动作的封装可以帮我们把一段公共的代码提取出来 语法如下 def 函数名(形参): 函数体 函数名(实参) # 函数名() def yue(): print(" ...

  8. 【转】Qt鼠标键盘事件

    http://blog.csdn.net/lovebird_27/article/details/50351336 Qt 程序需要在main()函数创建一个QCoreApplication对象,然后调 ...

  9. [转][C++]佛祖保佑,永无bug。C++ BUG解决方案

    // // _oo0oo_ // o8888888o // 88" . "88 // (| -_- |) // 0\ = /0 // ___/`---'\___ // .' \\| ...

  10. [原][粒子特效][spark]粒子系统system、主节点group、渲染器render

    深入浅出spark粒子特效连接:https://www.cnblogs.com/lyggqm/p/9956344.html system: A class defining a complete sy ...