Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27926 Accepted: 10805

Description

Input

Output

Sample Input

5 4
PHPP
PPHH
PPPP
PHPP
PHHP

Sample Output

6

Source

1 #include<iostream>
2 #include<cstdio>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<map>
7 #include<queue>
8 #include<stack>
9 #include<vector>
10 #include<set>
11 #define ll __int64
12 #define mod 100000000
13 using namespace std;
14 int n,m;
15 int a[70];
16 int b[110];
17 int dp[110][70][70];
18 char w[105][105];
19 int cnt=0;
20 int bit[70];
21 int fin (int x)
22 {
23     int jishu=0;
24     while(x>0)
25     {
26         if(x%2==1)
27             jishu++;
28         x/=2;
29     }
30     return jishu;
31 }
32 bool check(int x)//判断每一行可行的部署方案
33 {
34     if(x&(x/2)) return false;
35     if(x&(x/4)) return false;
36     return true;
37 }
38 bool fun(int x,int k)//判断是否满足平原的要求
39 {
40     if(x&b[k]) return false;
41     else return true;
42 }
43 int main()
44 {
45     scanf("%d %d",&m,&n);
46     char exm;
47     cnt=0;
48     for(int i=0; i<(1<<n); i++){
49         if(check(i)){
50             a[++cnt]=i;
51             bit[cnt]=fin(i);
52             }
53     }
54     memset(b,0,sizeof(b));
55     for(int i=1; i<=m; i++)
56         scanf("%s",w[i]+1);
57     for(int i=1; i<=m; i++)
58         for(int j=1; j<=n; j++)
59             if(w[i][j]=='H')
60                 b[i]+=(1<<(n-j));
61     memset(dp,-1,sizeof(dp));
62     for(int i=1; i<=cnt; i++){
63         if(fun(a[i],1))
64             dp[1][1][i]=bit[i];
65     }
66
67     for(int i=2; i<=m; i++){
68         for(int k=1; k<=cnt; k++){
69             if(!fun(a[k],i)) continue;
70             for(int j=1; j<=cnt; j++){
71                 if(a[k]&a[j]) continue;
72                 for(int l=1;l<=cnt;l++){
73                     if(a[k]&a[l]) continue;
74                     if(dp[i-1][j][l]==-1) continue;
75                  dp[i][l][k]=max(dp[i][l][k],dp[i-1][j][l]+bit[k]);
76                 }
77             }
78         }
79     }
80     int ans=0;
81     for(int i=1;i<=m;i++)
82     for(int j=1;j<=cnt;j++)
83     for(int l=1;l<=cnt;l++)
84     ans=max(ans,dp[i][j][l]);
85     printf("%d\n",ans);
86     return 0;
87 }