经典状态dp
代码:
#include<iostream>
#include<fstream>
#include<cmath>using namespace std;int n,m;char map[101][11];int state[101][1024];
int num[101];
int value[1024];
int maxx;int ok(int s,int t){int i,j,k;for(i=0;i<m;){j=t&(1<<i);if(j){if(map[s][i+1]=='H') return 0;if(i+1<m&&(t&(1<<(i+1)))) return 0;if(i+2<m&&(t&(1<<(i+2)))) return 0;i+=3;}elsei++;}return 1;
}void init(){int i,j,k;for(i=1;i<=n;i++)for(j=0;j<maxx;j++)if(ok(i,j)){state[i][num[i]]=j;num[i]++;}for(i=0;i<maxx;i++){for(j=0;j<m;j++)if(i&(1<<j))value[i]++;}}int ok2(int s,int t){int i,j,k;for(i=0;i<m;i++){j=s&(1<<i);k=t&(1<<i);if(j&&k) return 0;}return 1;
}int dp[101][1024][1024];void read(){
// ifstream cin("in.txt");int i,j,k,t;char c;cin>>n>>m;for(i=1;i<=n;i++)for(j=1;j<=m;j++)cin>>map[i][j];maxx=pow(2.,m);init();if(n==1){int ans=0;for(i=0;i<num[1];i++)ans=max(ans,value[state[1][i]]);cout<<ans<<endl;return;}for(i=0;i<num[1];i++)for(j=0;j<num[2];j++)if(ok2(state[1][i],state[2][j]))dp[2][j][i]=value[state[1][i]]+value[state[2][j]];for(i=3;i<=n;i++)for(j=0;j<num[i];j++)for(k=0;k<num[i-1];k++)if(ok2(state[i][j],state[i-1][k])){for(t=0;t<num[i-2];t++)if(ok2(state[i][j],state[i-2][t])&&ok2(state[i-1][k],state[i-2][t]))dp[i][j][k]=max(dp[i][j][k],dp[i-1][k][t]+value[state[i][j]]);}int ans=0;for(i=0;i<num[n];i++)for(j=0;j<num[n-1];j++)ans=max(ans,dp[n][i][j]);cout<<ans<<endl;}int main(){read();return 0;
}