Test instructions: n*m map ' H ' can put ' P ' can't put, deployed the grid up and down can not lay soldiers, to you map to find the maximum number of soldiers
Analysis: Related to the first two lines, so dp[i][j][k] the first row of the state is the J,i-1 row state is k when the maximum number of soldiers, the first to find out all feasible state, statistics of its number of soldiers.
#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 fre Open ("In.txt", "R", stdin) const LL INFLL = 0x3f3f3f3f3f3f3f3fll;const int inf= 0x7ffffff;const int mod = 1000000007;int N,m,a[200],dp[110][200][200];int num,sum[2000],cas[2000];bool judge (int x) {return (x<<1) &x) | | ((x<<2) &x) | | ((x>>1) &x) | | ((x>>2) &x);} int countnum (int x) {int t=0; while (x) {if (x&1) t++; x>>=1; } return t;} To preprocess all possible states void Init () {num=0; for (int i=0;i< (1<<m); ++i) {if (!judge (i)) {cas[num]=i; Sum[num++]=countnum (i); }}}void Solve () {init (); Memset (Dp,0,sizeof (DP)); for (int i=0;i<num;++i) {if (a[0]&cas[i]) continue; Dp[0][i][0]=sum[i]; } for (int i=1;i<n;++i) {//enumerates the current state for (int j=0;j<num;++j) {if (cas[j]&a[i]) continue; Enumerates the previous line of the condition for (int k=0;k<num;++k) {if (cas[k]&a[i-1]) continue; if (Cas[k]&cas[j]) continue; int maxv=-1; for (int l=0;l<num;++l) {if (Cas[l]&cas[k]) | | (Cas[l]&cas[j])) Continue Maxv=max (Maxv,dp[i-1][k][l]); } Dp[i][j][k]=maxv+sum[j]; }}} int maxn=-1; for (int i=0;i<num;++i) for (int j=0;j<num;++j) {Maxn=max (maxn,dp[n-1][i][j]); } printf ("%d\n", MAXN);} int main () {char ch; while (~SCANF ("%d%d", &n,&m)) {memset (a,0,sizeof (a)); for (int i=0;i<n;++i) for (int j=0;j<m;++j) {cin>>ch; if (ch== ' H ') {a[i]|= (1<<j); }} solve (); }return 0;}
Artillery positions (POJ 1185 pressure DP)