您的位置:首页 > 其它

[状压dp]HDOJ1565 方格取数(1)

2014-11-07 21:25 148 查看
中文题~~ 题意略

$n\le 20$ ! 很明显是状压!

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cctype>
#include <cmath>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
typedef long long LL;
typedef long double LD;
#define pi acos(-1.0)
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
typedef pair<int, int> PI;
typedef pair<int, PI> PP;
#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;}
//inline int read(){char ch=' ';int ans=0;while(ch<'0' || ch>'9')ch=getchar();while(ch<='9' && ch>='0'){ans=ans*10+ch-'0';ch=getchar();}return ans;}
//inline void print(LL x){printf(LLD, x);puts("");}
//inline void read(double &x){char c = getchar();while(c < '0') c = getchar();x = c - '0'; c = getchar();while(c >= '0'){x = x * 10 + (c - '0'); c = getchar();}}

int mp[25][25];
int dp[2][1<<20];   // n格为一个状态
int p[1<<20], d;
void pre()       //先记录所有的合法状态 即不相邻
{
d=0;
for(int i=0;i<(1<<20);i++)
if((i & (i<<1))==0)
p[d++]=i;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
pre();
int n;
while(~scanf("%d", &n))
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d", &mp[i][j]);
memset(dp, 0, sizeof(dp));
int cur=0;
for(int i=0;i<n;i++)
{
for(int k=0;k<d;k++)
{
int sum=0;
if(p[k]>=(1<<n))    // 算是优化吧 没有就tle了
break;
for(int j=0;j<n;j++)
if(p[k] & (1<<j))
sum+=mp[i][j];
for(int j=0;j<d;j++)
{
if(p[j]>=(1<<n))   // 算是优化吧 没有就tle了
break;
if((p[k] & p[j])==0)
dp[cur][p[k]]=max(dp[cur][p[k]], dp[cur^1][p[j]]+sum);
}
}
cur^=1;
}
cur^=1;
int ans=0;
for(int i=0;i<(1<<n);i++)
ans=max(ans, dp[cur][i]);
printf("%d\n", ans);
}
return 0;
}


HDOJ 1565
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: