首页 技术 正文
技术 2022年11月13日
0 收藏 1000 点赞 3,000 浏览 1342 个字

题意:

给你一个n*n的格子的棋盘,每个格子里面有一个非负数。 
从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出的数的和最大。 分析:直接枚举压缩后的所有情况超时,所以先把行所有可能的情况处理并得到该情况的对应的和,状态只与上一行状态有关,所有用两个数组保存当前行状态和上一行状态。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = 1000000007;
int n,now[20001],total[20001],a[20][20];
int nnum,par[20001],dp[20001],dp1[20001];
void state(int i,int k,int s,int sum){
if(k>=n){
now[++nnum]=s;
total[nnum]=sum;
return;
}
state(i,k+2,s|(1<<k),sum+a[i][k]);
state(i,k+1,s,sum);
}
void solve(){
for(int i=0;i<n;++i){
nnum=0;
state(i,0,0,0);
memset(dp,0,sizeof(dp));
for(int j=1;j<=nnum;++j)
for(int l=1;l<=nnum;++l)
if((now[j]&now[l])==0){
dp[j]=max(dp[j],dp1[l]+total[j]);
}
for(int l=1;l<=nnum;++l){
dp1[l]=dp[l];
}
}
int maxv=-1;
for(int i=1;i<=nnum;++i){
maxv=max(maxv,dp1[i]);
}
printf("%d\n",maxv);
}
int main()
{
while(~scanf("%d",&n)){
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
scanf("%d",&a[i][j]);
memset(dp1,0,sizeof(dp1));
solve();
}
return 0;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,906
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,739
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,492
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,130
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,293