# 状压DP 入门题

Description

Input

Output

Sample Input
3
75 15 21
75 15 28
34 70 5
Sample Output
188

1.先枚举所有状态（从0到1<< n）找出不含相邻1的状态，那么这就是满足每一行不相邻的所有合法状态。
2.特殊处理第1行的状态。上一步找到的合法状态都可以作为是第1行的状态。
3.枚举后面的行。后面的行要满足2个条件。一就是没有相邻的1。二是和上一行的1不冲突。找到合法的状态，利用dp求出最大值就好了。

ac代码：

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
using namespace std;
int dp[25][18000];
int num[25][18000];
int state[18000];
int mapp[25][25];
int N,top,total;
void init()
{
top=0;
int i;
total=1<<N;
for(i=0;i<total;i++)
{
if((i&i<<1))
continue;
state[++top]=i;
}
}
int fit(int x,int k) //计算x状态，在k行的和。
{
int sum=0;
for(int i=1;i<=N;i++)
{
if((x>>(i-1)&1))
sum+=mapp[k][i];
}
return sum;
}
int main()
{
while(cin>>N)
{
int i,j,k,z;
memset(dp,0,sizeof(dp));
for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
{
cin>>mapp[i][j];
}
}
init();
for(i=1;i<=top;i++)
{
num[1][i]=dp[1][i]=fit(state[i],1);
for(j=2;j<=N;j++)
{
num[j][i]=fit(state[i],j);
}
}
for(i=2;i<=N;i++)
{
for(j=1;j<=top;j++)   //遍历第i行的状态
{
for(k=1;k<=top;k++)//遍历第i-1行的状态
{
if(state[j]&state[k])
continue;
dp[i][j]=max(dp[i][j],dp[i-1][k]+num[i][j]);
}
}
}
int maxx=-1;
for(i=1;i<=top;i++)
{
maxx=max(maxx,dp[N][i]);
}
cout<<maxx<<endl;
}
return 0;
}

Description
Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can’t be planted. Canny FJ knows that the cows dislike eating close to each other, so when choosing which squares to plant, he avoids choosing squares that are adjacent; no two chosen squares share an edge. He has not yet made the final choice as to which squares to plant.

Being a very open-minded man, Farmer John wants to consider all possible options for how to choose the squares for planting. He is so open-minded that he considers choosing no squares as a valid option! Please help Farmer John determine the number of ways he can choose the squares to plant.

Input
Line 1: Two space-separated integers: M and N
Lines 2.. M+1: Line i+1 describes row i of the pasture with N space-separated integers indicating whether a square is fertile (1 for fertile, 0 for infertile)
Output
Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.
Sample Input
2 3
1 1 1
0 1 0
Sample Output
9
Hint
Number the squares as follows:
1 2 3
4

There are four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on two squares (13, 14, or 34), 1 way to plant on three squares (134), and one way to plant on no squares. 4+3+1+1=9.

ac代码：

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
using namespace std;
int N,M;
int top,total;
int state[1000];  //满足不相邻的状态。
int dp[20][1000];
int cur[20];   //地图。
void init()
{
total=1<<M;
top=0;
for(int i=0;i<total;i++)
{
if(i&(i<<1))
continue;
state[++top]=i;
}
}
bool fit(int x,int k) //判断状态x与第k行的地图是否符合。
{
if(x&cur[k])
return 0;
return 1;
}
int main()
{
while(~scanf("%d%d",&N,&M))
{
int i,j,k,mapp;
memset(dp,0,sizeof(dp));
memset(cur,0,sizeof(cur));
memset(state,0,sizeof(state));
init();
for(i=1;i<=N;i++) //把地图打表。
{
for(j=1;j<=M;j++)
{
cin>>mapp;
if(mapp==0)
cur[i]+=1<<(M-j);
}
}
for(i=1;i<=top;i++)
{
if(fit(state[i],1))
dp[1][i]=1;
}
for(i=2;i<=N;i++)
{
for(j=1;j<=top;j++)
{
if(!fit(state[j],i))
continue;
//再看是不是和上一行冲突。
for(k=1;k<=top;k++)
{
if(!fit(state[k],i-1))
continue;
if(state[k]&state[j])
continue;
dp[i][j]+=dp[i-1][k];
}
}
}
int sum=0;
for(k=1;k<=top;k++)
{
sum=(sum+dp[N][k])%100000000;
}
cout<<sum<<endl;
}
return 0;
}