标签:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 25647 | Accepted: 9892 |
Description
Input
Output
Sample Input
5 4 PHPP PPHH PPPP PHPP PHHP
Sample Output
6
1 #include <map> 2 3 #include <cstdio> 4 5 #include <iostream> 6 7 #include <string.h> 8 9 #include <algorithm> 10 11 using namespace std; 12 13 const int maxn=100; 14 15 int n,m,cnt,ans; 16 17 char ch; 18 19 int a[maxn+1]; 20 21 int vald[maxn+1][1<<10+1]; 22 23 int valdcount[maxn+1]; 24 25 int dp[maxn+1][maxn+10][maxn+10]; 26 27 //map<int,int> ma[maxn+10]; 28 29 bool judge(int x){ 30 return (!(x&(x<<1))&&!(x&(x<<2))); 31 } 32 33 int BitCount(unsigned int n) 34 { 35 unsigned int c =0 ; 36 for (c =0; n; ++c) 37 { 38 n &= (n -1) ; 39 } 40 return c ; 41 } 42 43 int main() 44 { 45 scanf("%d%d",&n,&m); 46 for(int i=1;i<=n;i++){ 47 for(int j=1;j<=m;j++){ 48 scanf(" %c",&ch); 49 if(ch==‘H‘){ 50 a[i]|=(1<<(j-1)); 51 } 52 } 53 } 54 for(int i=1;i<=n;i++){ 55 cnt=0; 56 for(int j=0;j<=((1<<m)-1);j++){ 57 if((!(j&a[i]))&&judge(j)){ 58 vald[i][cnt]=j; 59 //ma[i][j]=cnt; 60 cnt++; 61 } 62 } 63 valdcount[i]=cnt-1; 64 } 65 for(int i=0;i<=valdcount[1];i++){ 66 int x=vald[1][i]; 67 for(int j=0;j<=(1<<(m)-1);j++) dp[1][i][j]=BitCount(x); 68 } 69 for(int i=0;i<=valdcount[2];i++){ 70 int x=vald[2][i]; 71 int q=BitCount(x); 72 for(int j=0;j<=valdcount[1];j++) { 73 int y=vald[1][j]; 74 if(!(x&y)) dp[2][i][j]=max(dp[2][i][j],q+BitCount(y)); 75 } 76 } 77 for(int i=3;i<=n;i++){ 78 for(int j=0;j<=valdcount[i];j++){ 79 int x=vald[i][j]; 80 int q=BitCount(x); 81 for(int k=0;k<=valdcount[i-1];k++){ 82 int y=vald[i-1][k]; 83 for(int h=0;h<=valdcount[i-2];h++){ 84 int z=vald[i-2][h]; 85 if(!(x&y)&&!(y&z)&&!(x&z)){ 86 dp[i][j][k]=max(dp[i][j][k],dp[i-1][k][h]+q); 87 } 88 } 89 } 90 } 91 } 92 for(int i=0;i<=valdcount[n];i++){ 93 int x=vald[n][i]; 94 for(int j=0;j<=valdcount[n-1];j++) { 95 ans=max(ans,dp[n][i][j]); 96 } 97 } 98 printf("%d\n",ans); 99 return 0; 100 }
标签:
原文地址:http://www.cnblogs.com/GeniusYang/p/5746133.html