DFS CCPC2017 南宁I题

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(1t200)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(1k3). 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,回溯,感觉没太多好讲的

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const double pi=acos(-1);
 5 const int mod=1e9+7;
 6 const int inf=1<<30;
 7 const int maxn=1e5+7;
 8 int a[4][4];
 9 int k;
10 int val(int i,int j){
11     return a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1];
12 }
13 void rotat(int i,int j){
14     int a1=a[i][j],a2=a[i][j+1],a3=a[i+1][j],a4=a[i+1][j+1];
15     a[i][j]=a2,a[i][j+1]=a4,a[i+1][j]=a1,a[i+1][j+1]=a3;
16 }
17 void retat(int i,int j){
18     int a1=a[i][j],a2=a[i][j+1],a3=a[i+1][j],a4=a[i+1][j+1];
19     a[i][j]=a3,a[i][j+1]=a1,a[i+1][j]=a4,a[i+1][j+1]=a2;
20 }
21 int dfs(int t){
22     int ans;
23     if(t==2*k-1){
24         ans=inf;
25         for(int i=0;i<3;i++)
26         for(int j=0;j<3;j++)
27         ans=min(ans,val(i,j));
28         return ans;
29     }
30     else if(t%2==0){
31         ans=0;
32         for(int i=0;i<3;i++)
33         for(int j=0;j<3;j++){
34             rotat(i,j);
35             int tmp=val(i,j);
36             ans=max(ans,tmp+dfs(t+1));
37             retat(i,j);
38         }
39     }
40     else{
41         ans=inf;
42         for(int i=0;i<3;i++)
43         for(int j=0;j<3;j++){
44             rotat(i,j);
45             int tmp=val(i,j);
46             ans=min(ans,tmp+dfs(t+1));
47             retat(i,j);
48         }
49     }
50     return ans;
51 }
52 int main(){
53     int T;scanf("%d",&T);
54     while(T--){
55         scanf("%d",&k);
56         for(int i=0;i<4;i++){
57             for(int j=0;j<4;j++){
58                 scanf("%d",&a[i][j]);
59             }
60         }
61         cout<<dfs(0)<<endl;
62     }
63     return 0;
64 }

猜你喜欢

转载自www.cnblogs.com/qingjiuling/p/10407196.html
今日推荐