HDU 4539郑厂长系列故事――排兵布阵(状压DP)

时间:2021-06-30 08:45:26

HDU 4539  郑厂长系列故事――排兵布阵

基础的状压DP,首先记录先每一行可取的所哟状态(一行里互不冲突的大概160个状态),

直接套了一个4重循环居然没超时我就呵呵了

 //#pragma comment(linker,"/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1e8
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
//typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
//const LL MOD = 1000000007; int N, M;
int ma[];
int dp[][][]; int stNum;
int st[], num[]; int judge(int x)
{
if(x & (x<<)) return ;
return ;
} int get1(int x)
{
int ret = ;
while(x)
{
ret += x & ;
x >>= ;
}
return ret;
} void init()
{
mem1(dp);mem0(ma);
stNum = ;
for(int i=;i<(<<M);i++) if(judge(i))
{
st[stNum] = i;
num[stNum++] = get1(i);
}
} int main()
{
//FOPENIN("in.txt");
while(~scanf("%d %d%*c", &N, &M))
{
init();
int x, cur = ;
for(int i=;i<=N;i++)
for(int j=;j<M;j++) {
scanf("%d", &x);
if(x==) ma[i] |= (<<j);
}
for(int i=;i<stNum;i++) {
if(st[i] & ma[]) continue;
dp[cur][i][] = num[i];
}
for(int r = ; r <= N; r ++)
{
cur = !cur;
for(int i = ; i < stNum; i ++)
{
if(st[i] & ma[r]) continue;
for(int j = ; j < stNum; j ++ )
{
if(st[j] & ma[r-]) continue;
int p = (st[i]<<) | (st[i]>>);
if(st[j] & p) continue;
for(int k = ; k < stNum; k ++) if(dp[!cur][j][k] != -)
{
int pp = st[i] | ((st[j]<<) | (st[j]>>));
if(st[k] & pp) continue;
dp[cur][i][j] = MAX(dp[cur][i][j], dp[!cur][j][k] + num[i]);
} }
}
}
int ans = ;
for(int i=;i<stNum;i++) for(int j=;j<stNum;j++)
ans = MAX(ans, dp[cur][i][j]);
printf("%d\n", ans);
}
return ;
} /* 6
3
2
12 */