Codeforces-GYM/101853:Maximum Sum(轮廓线DP)

E. Maximum Sum
time limit per test2.0 s
memory limit per test512 MB

inputstandard input
outputstandard output
You are given a grid consisting of n rows each of which is dived into n columns. The rows are numbered from 1 to n from top to bottom, and the columns are numbered from 1 to n from left to right. Each cell is identified by a pair (x,y) ( x , y ) , which means that the cell is located in the row x and column y. All cells in the grid contain positive integers.

Your task is to choose a subset of the grid’s cells, such that their summation is as maximal as possible, and there are no two adjacent cells in that subset. Two cells are considered adjacent if they are horizontal, vertical, or diagonal neighbors.

Input
The first line contains an integer T(1T100) T ( 1   ≤   T   ≤   100 ) , in which T T is the number of test cases.

The first line contains an integer n(1n16), in which n is the number of rows and columns in the grid.

Then n lines follow, each line contains n integers, giving the grid. All values in the grid are between 1 and 1000 (inclusive).

Output
For each test case, print a single line containing the maximum sum of a subset of the grid’s cells. The chosen subset must not contain any adjacent cells.

Example
input
2
2
4 7
2 9
3
1 2 3
4 5 6
7 8 9
output
9
20

思路:轮廓线DP模版化的题。

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+10;
const int MOD=998244353;
const int INF=1e9+7;
const double PI=acos(-1.0);
typedef long long ll;
int d[2][1<<17];    //二维滚动数组
int a[20][20];
int n;
int g(int x){return x&(1<<(n+1))?x^(1<<(n+1)):x;}//对1<<(n+1)取模
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)scanf("%d",&a[i][j]);
        memset(d,0,sizeof d);
        int now=1,pre=0;
        for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<(1<<(n+1));k++)
            {
                d[now][g(k<<1)]=max(d[now][g(k<<1)],d[pre][k]);//不选当前这个数
                //判断是否能选当前这个数
                int tag=0;
                if(i==0&&j==0)tag=1;
                if(i==0&&j&&(k&1)==0)tag=1;
                if(i&&j==0&&(k&(1<<(n-1)))==0&&(k&(1<<(n-2)))==0)tag=1;
                if(i&&j==n-1&&(k&1)==0&&(k&(1<<n))==0&&(k&(1<<(n-1)))==0)tag=1;
                if(i&&j&&j<n-1&&(k&1)==0&&(k&(1<<n))==0&&(k&(1<<(n-1)))==0&&(k&(1<<(n-2)))==0)tag=1;

                if(tag)d[now][g((k<<1)^1)]=max(d[now][g((k<<1)^1)],d[pre][k]+a[i][j]);//选当前这个数
            }
            memset(d[pre],0,sizeof d[pre]);
            now^=1;
            pre^=1;
        }
        printf("%d\n",*max_element(d[pre],d[pre]+(1<<(n+1))));
    }
    return 0;
}
您提供的链接是Codeforces的一个问题,问题编号为104377。Codeforces是一个知名的在线编程竞赛平台,经常举办各种编程比赛和训练。GymCodeforces的一个扩展包,用于组织私人比赛和训练。您提供的链接指向了一个问题的页面,但具体的问题内容和描述无法通过链接获取。如果您有具体的问题或需要了解关于Codeforces Gym的更多信息,请提供更详细的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [http://codeforces.com/gym/100623/attachments E题](https://blog.csdn.net/weixin_30820077/article/details/99723867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [http://codeforces.com/gym/100623/attachments H题](https://blog.csdn.net/weixin_38166726/article/details/99723856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [CodeforcesPP:Codeforces扩展包](https://download.csdn.net/download/weixin_42101164/18409501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值