N*m
P and M
P can put a man m not
People can't call each other to ask how many people they can put up.
#include <stdio.h>#include<algorithm>#include<string.h>#include<math.h>#include<map>using namespacestd;#defineMAXN 1<<10Charz[ the][ the];intx[ the];intdp[ the][ $][ $];intnum[ $];intcn[ $];intCount (inta) { intans=0; while(a>0) { if(a&1) ans++; A=a>>1; } returnans;}intMain () {intn,m; while(SCANF ("%d%d", &n,&m)! =EOF) { for(intI=0; i<n;i++) scanf ("%s", Z[i]); memset (x,0,sizeof(x)); Memset (DP,-1,sizeof(DP)); intCnt=0; inten=1<<m; for(intI=0; i<en;i++) if((i& (i<<2))|| (i& (i<<1)))==0) {cn[cnt]=Count (i); Num[cnt++]=i; } for(intI=0; i<n;i++) { for(intj=0; j<m;j++) if(z[i][j]=='H') X[i]|=(1<<j); } for(intI=0; i<cnt;i++) { if(x[0]&Num[i])Continue; dp[0][i][0]=Cn[i]; } for(intI=1; i<n;i++)//Line { for(intj=0; j<cnt;j++)//The state of this line { if(x[i]&num[j])//cannot contradictContinue; for(intk=0; k<cnt;k++)//the state of the previous row { if(num[j]&num[k])//Adjacent two lines cannot appear simultaneously 1Continue; for(intk1=0; k1<cnt;k1++)//The second line above the state { if(num[j]&NUM[K1])//Adjacent two lines cannot appear simultaneously 1Continue; if(dp[i-1][k][k1]==-1) Continue; DP[I][J][K]=max (dp[i][j][k],dp[i-1][k][k1]+Cn[j]); } } } } intans=0; for(intI=0; i<cnt;i++) for(intj=0; j<cnt;j++) ans=max (dp[n-1][i][j],ans); printf ("%d\n", ans); } return 0;}
Artillery position POJ 1185