Release the explanation later...
Int st [66]; int cur [66]; char STR [110] [15]; int DP [110] [66] [66]; // DP [I] [J] [k] indicates the maximum number of artillery units when the state of row I is J and the state of the I-1 is K. Int num [66]; int N, m; int tot; bool OK (INT X) {If (X & (x <1) return 0; If (X & (x <2) return 0; return 1;} void Init () {int I, j; Tot = 0; for (I = 0; I <(1 <m); I ++) {If (OK (I) ST [tot ++] = I ;}} int count (INT X) {int CNT = 0; while (X) {If (X & 1) CNT ++; X >>= 1 ;}return CNT ;}bool fit (int x, int y) {If (X & Y) return 0; R Eturn 1;} int main () {While (scanf ("% d", & N, & M )! =-1) {int I, j, k; Init (); for (I = 0; I <n; I ++) {scanf ("% s ", STR [I]); cur [I] = 0; For (j = 0; j <m; j ++) {If (STR [I] [J] = 'H') {cur [I] + = (1 <j) ;}} memset (DP,-1, sizeof (DP); for (I = 0; I <tot; I ++) {num [I] = count (ST [I]); if (FIT (ST [I], cur [0]) {DP [0] [I] [0] = num [I] ;}} for (INT r = 1; r <n; r ++) {for (I = 0; I <tot; I ++) {If (! FIT (cur [R], St [I]) continue; For (j = 0; j <tot; j ++) {If (ST [I] & St [J]) continue; For (k = 0; k <tot; k ++) {If (ST [I] & St [k]) continue; If (DP [r-1] [J] [k]! =-1) {DP [r] [I] [J] = max (DP [r] [I] [J], DP [r-1] [J] [k] + num [I]) ;}}}int ans = 0; for (I = 0; I <tot; I ++) {for (j = 0; j <tot; j ++) {ans = max (ANS, DP [n-1] [I] [J]);} printf ("% d \ n", ANS);} return 0 ;}