[State-compressed DP] poj 1185

Source: Internet
Author: User

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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.