Link: http://poj.org/problem? Id = 1185
A map has two kinds of terrain: H and P. A gun can be placed on P. The attack ranges from top to bottom, from left to right, the question is how many guns can be placed on a map at most. Row n <= 100, column M <= 10.
Idea: Because guns cannot be placed in the upper, lower, and lower layers, the number of States in each row is reduced from 2 ^ 10 to 60. The state transition equation is DP [I] [J] [k] = max (DP [I-1] [k] [l] + BB [J]). DP [I] [J] [k] indicates the total number of Turret points placed in the first line of I in the state of J and in the first line of the I-1 in the state of K. Bb [J] indicates the number of Turret in the whole row of status K. After hitting this question, I began to be familiar with the pressure DP.
Code:
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<map>#include<cstdlib>#include<queue>#include<stack>#include<vector>#include<ctype.h>#include<algorithm>#include<string>#define PI acos(-1.0)#define maxn 10005#define INF 0x7ffffffftypedef long long ll;using namespace std;int judge(int x){ if((x&(x<<1))==0&&((x&(x<<2))==0)&&(((x<<1)&(x<<2))==0)) return 1; return 0;}int main(){ int row[105]={0}; int aa[105]={0}; int bb[105]={0}; int top=0; char land[105][15]; int dp[105][60][60]={0}; int r,c; scanf("%d%d",&r,&c); for(int i=0;i<(1<<c);i++) { if(judge(i)) { int k=i; while(k) { if(k%2==1) bb[top]++; k/=2; } aa[top++]=i; } } for(int i=0;i<r;i++) { scanf("%s",land[i]); for(int j=0;j<c;j++) { row[i]*=2; if(land[i][j]=='P') row[i]++; } } for(int i=0;i<top;i++) { if((aa[i]&row[0])==aa[i]) { dp[0][i][0]=bb[i]; } } for(int i=0;i<top;i++) { for(int j=0;j<top;j++) { if((aa[i]&aa[j])==0&&(aa[i]&row[1])==aa[i]&&(aa[j]&row[0])==aa[j]) dp[1][i][j]=dp[0][j][0]+bb[i]; } } for(int i=2;i<r;i++) { for(int j=0;j<top;j++) { for(int k=0;k<top;k++) { for(int l=0;l<top;l++) { if((aa[j]&aa[k])==0&&(aa[l]&aa[j])==0&&(aa[l]&aa[k])==0&&(aa[j]&row[i])==aa[j]&&(aa[k]&row[i-1])==aa[k]&&(aa[l]&row[i-2])==aa[l]) dp[i][j][k]=max(dp[i][j][k],dp[i-1][k][l]+bb[j]); } } } } int ans=0; for(int i=0;i<top;i++) { for(int j=0;j<top;j++) { if(dp[r-1][i][j]>ans) ans=dp[r-1][i][j]; } } printf("%d\n",ans);}
Poj 1185 artillery distributed pressure DP